package admin

import (
	"quyun/app/models"
	"quyun/app/requests"
	"quyun/database/schemas/public/model"

	"github.com/gofiber/fiber/v3"
)

type UserListQuery struct {
	Keyword *string `query:"keyword"`
}

// @provider
type users struct{}

// List users
//
//	@Router	/admin/users [get]
//	@Bind	pagination query
//	@Bind	query query
func (ctl *users) List(ctx fiber.Ctx, pagination *requests.Pagination, query *UserListQuery) (*requests.Pager, error) {
	cond := models.Users.BuildConditionWithKey(query.Keyword)
	return models.Users.List(ctx.Context(), pagination, cond)
}

// Show user
//
//	@Router	/admin/users/:id [get]
//	@Bind	id path
func (ctl *users) Show(ctx fiber.Ctx, id int64) (*model.Users, error) {
	return models.Users.GetByID(ctx.Context(), id)
}

// Articles show user bought articles
//
//	@Router	/admin/users/:id/articles [get]
//	@Bind	id path
//	@Bind	pagination query
func (ctl *users) Articles(ctx fiber.Ctx, id int64, pagination *requests.Pagination) (*requests.Pager, error) {
	return models.Posts.Bought(ctx.Context(), id, pagination)
}

type UserBalance struct {
	Balance int64 `json:"balance"`
}

// Balance
//
//	@Router	/admin/users/:id/balance [post]
//	@Bind	id path
//	@Bind	balance body
func (ctl *users) Balance(ctx fiber.Ctx, id int64, balance *UserBalance) error {
	return models.Users.AddBalance(ctx.Context(), id, balance.Balance)
}
