package admin

import (
    "net/http"
    "strconv"

    "bindbox-game/internal/code"
    "bindbox-game/internal/pkg/core"
    "bindbox-game/internal/pkg/validation"
    bannersvc "bindbox-game/internal/service/banner"
)

type createBannerRequest struct {
    Title    string `json:"title" binding:"required"`
    ImageURL string `json:"image_url" binding:"required"`
    LinkURL  string `json:"link_url"`
    Sort     int32  `json:"sort"`
    Status   int32  `json:"status"`
}

type createBannerResponse struct {
    ID      int64  `json:"id"`
    Message string `json:"message"`
}

// CreateBanner 创建轮播图
// @Summary 创建轮播图
// @Tags 管理端.运营
// @Accept json
// @Produce json
// @Param RequestBody body createBannerRequest true "请求参数"
// @Success 200 {object} createBannerResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/banners [post]
// @Security LoginVerifyToken
func (h *handler) CreateBanner() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(createBannerRequest)
        res := new(createBannerResponse)
        if err := ctx.ShouldBindJSON(req); err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
            return
        }
        if ctx.SessionUserInfo().IsSuper != 1 {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作"))
            return
        }
        item, err := h.banner.Create(ctx.RequestContext(), bannersvc.CreateInput{Title: req.Title, ImageURL: req.ImageURL, LinkURL: req.LinkURL, Sort: req.Sort, Status: req.Status})
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
            return
        }
        res.ID = item.ID
        res.Message = "操作成功"
        ctx.Payload(res)
    }
}

type modifyBannerRequest struct {
    Title    *string `json:"title"`
    ImageURL *string `json:"image_url"`
    LinkURL  *string `json:"link_url"`
    Sort     *int32  `json:"sort"`
    Status   *int32  `json:"status"`
}

// ModifyBanner 修改轮播图
// @Summary 修改轮播图
// @Tags 管理端.运营
// @Accept json
// @Produce json
// @Param banner_id path string true "轮播图ID"
// @Param RequestBody body modifyBannerRequest true "请求参数"
// @Success 200 {object} pcSimpleMessage
// @Failure 400 {object} code.Failure
// @Router /api/admin/banners/{banner_id} [put]
// @Security LoginVerifyToken
func (h *handler) ModifyBanner() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(modifyBannerRequest)
        if err := ctx.ShouldBindJSON(req); err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
            return
        }
        idStr := ctx.Param("banner_id")
        id, _ := strconv.ParseInt(idStr, 10, 64)
        if ctx.SessionUserInfo().IsSuper != 1 {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作"))
            return
        }
        if err := h.banner.Modify(ctx.RequestContext(), id, bannersvc.ModifyInput{Title: req.Title, ImageURL: req.ImageURL, LinkURL: req.LinkURL, Sort: req.Sort, Status: req.Status}); err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
            return
        }
        ctx.Payload(pcSimpleMessage{Message: "操作成功"})
    }
}

// DeleteBanner 删除轮播图
// @Summary 删除轮播图
// @Tags 管理端.运营
// @Accept json
// @Produce json
// @Param banner_id path string true "轮播图ID"
// @Success 200 {object} pcSimpleMessage
// @Failure 400 {object} code.Failure
// @Router /api/admin/banners/{banner_id} [delete]
// @Security LoginVerifyToken
func (h *handler) DeleteBanner() core.HandlerFunc {
    return func(ctx core.Context) {
        idStr := ctx.Param("banner_id")
        id, _ := strconv.ParseInt(idStr, 10, 64)
        if ctx.SessionUserInfo().IsSuper != 1 {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作"))
            return
        }
        if err := h.banner.Delete(ctx.RequestContext(), id); err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
            return
        }
        ctx.Payload(pcSimpleMessage{Message: "操作成功"})
    }
}

type listBannersRequest struct {
    Status   *int32 `form:"status"`
    Page     int    `form:"page"`
    PageSize int    `form:"page_size"`
}

type bannerItem struct {
    ID       int64  `json:"id"`
    Title    string `json:"title"`
    ImageURL string `json:"image_url"`
    LinkURL  string `json:"link_url"`
    Sort     int32  `json:"sort"`
    Status   int32  `json:"status"`
}

type listBannersResponse struct {
    Page     int          `json:"page"`
    PageSize int          `json:"page_size"`
    Total    int64        `json:"total"`
    List     []bannerItem `json:"list"`
}

// ListBanners 查看轮播图列表
// @Summary 查看轮播图列表
// @Tags 管理端.运营
// @Accept json
// @Produce json
// @Param status query int false "状态"
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量" default(20)
// @Success 200 {object} listBannersResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/banners [get]
// @Security LoginVerifyToken
func (h *handler) ListBanners() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(listBannersRequest)
        res := new(listBannersResponse)
        if err := ctx.ShouldBindForm(req); err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
            return
        }
        items, total, err := h.banner.List(ctx.RequestContext(), bannersvc.ListInput{Status: req.Status, Page: req.Page, PageSize: req.PageSize})
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
            return
        }
        res.Page = req.Page
        res.PageSize = req.PageSize
        res.Total = total
        res.List = make([]bannerItem, len(items))
        for i, it := range items {
            res.List[i] = bannerItem{ID: it.ID, Title: it.Title, ImageURL: it.ImageURL, LinkURL: it.LinkURL, Sort: it.Sort, Status: it.Status}
        }
        ctx.Payload(res)
    }
}