package app

import (
    "net/http"

    "bindbox-game/internal/code"
    "bindbox-game/internal/pkg/core"
    "bindbox-game/internal/pkg/validation"
    bannersvc "bindbox-game/internal/service/banner"
    "bindbox-game/internal/pkg/logger"
    "bindbox-game/internal/repository/mysql"
    "bindbox-game/internal/repository/mysql/dao"
)

type bannerHandler struct {
    logger logger.CustomLogger
    readDB *dao.Query
    banner bannersvc.Service
}

func NewBanner(logger logger.CustomLogger, db mysql.Repo) *bannerHandler {
    return &bannerHandler{logger: logger, readDB: dao.Use(db.GetDbR()), banner: bannersvc.New(logger, db)}
}

type listAppBannersResponse struct {
    List []appBannerItem `json:"list"`
}

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

// ListBannersForApp APP端轮播图列表
// @Summary APP端轮播图列表
// @Tags APP端.运营
// @Accept json
// @Produce json
// @Success 200 {object} listAppBannersResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/banners [get]
func (h *bannerHandler) ListBannersForApp() core.HandlerFunc {
    return func(ctx core.Context) {
        res := new(listAppBannersResponse)
        items, err := h.banner.ListEnabled(ctx.RequestContext())
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, validation.Error(err)))
            return
        }
        res.List = make([]appBannerItem, len(items))
        for i, it := range items {
            res.List[i] = appBannerItem{ID: it.ID, Title: it.Title, ImageURL: it.ImageURL, LinkURL: it.LinkURL, Sort: it.Sort}
        }
        ctx.Payload(res)
    }
}