package api

import (
	"github.com/gin-gonic/gin"
	activityBiz "github.guxuan/haibei/internal/mods/activity/biz"
	"github.guxuan/haibei/internal/mods/app/biz"
	commonBiz "github.guxuan/haibei/internal/mods/common/biz"
	commonSchema "github.guxuan/haibei/internal/mods/common/schema"
	customerBiz "github.guxuan/haibei/internal/mods/customer/biz"
	customerSchema "github.guxuan/haibei/internal/mods/customer/schema"
	ProductBiz "github.guxuan/haibei/internal/mods/product/biz"

	"github.guxuan/haibei/pkg/util"
)

// Defining the `App` api.
type App struct {
	AppBIZ           *biz.App
	CustomerBIZ      *customerBiz.Customer
	BannerBIZ        *commonBiz.Banner
	ActivityBIZ      *activityBiz.Activity
	QuestionnaireBIZ *activityBiz.Questionnaire
	ProductBIZ       *ProductBiz.Product
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary 小程序登陆
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/login [post]
func (a *App) Login(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(customerSchema.CustomerLoginForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}
	data, err := a.CustomerBIZ.Login(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, data)
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary  发送验证码
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/sms/{phone} [get]
func (a *App) SendSms(c *gin.Context) {
	ctx := c.Request.Context()
	phone := c.Param("phone")
	data, err := a.CustomerBIZ.SendSms(ctx, phone)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, data)
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary 小程序绑定手机号
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/bind [post]
func (a *App) BindPhone(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(customerSchema.CustomerBindPhoneForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}
	data, err := a.CustomerBIZ.BindPhone(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, data)
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary 首页banner
// @Success 200 {object} util.ResponseResult{data=[]commonSchema.Banner}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/banner [get]
func (a *App) GetBannerList(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(commonSchema.BannerQueryParam)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}
	data, err := a.BannerBIZ.Query(ctx, *item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, data)
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary 首页接口
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/home [post]
func (a *App) HomeDetails(c *gin.Context) {
	ctx := c.Request.Context()
	uid, err := a.AppBIZ.GetLoginUid(ctx)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, uid)
}

// @Tags APP端
// @Security ApiKeyAuth
// @Summary 首页接口
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/apps/mine [post]
func (a *App) MineDetails(c *gin.Context) {
	//ctx := c.Request.Context()
}
