package api

import (
	"github.com/gin-gonic/gin"
	"github.guxuan/haibei/internal/mods/common/biz"
	"github.guxuan/haibei/internal/mods/common/schema"
	"github.guxuan/haibei/pkg/util"
)

// Defining the `SMSLog` api.
type SMSLog struct {
	SMSLogBIZ *biz.SMSLog
}

// @Tags 短信日志模块
// @Security ApiKeyAuth
// @Summary Query smsLog list
// @Param current query int true "pagination index" default(1)
// @Param pageSize query int true "pagination size" default(10)
// @Success 200 {object} util.ResponseResult{data=[]schema.SMSLog}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/smsLogs [get]
func (a *SMSLog) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.SMSLogQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}
	result, err := a.SMSLogBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags 短信日志模块
// @Security ApiKeyAuth
// @Summary Get smsLog record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.SMSLog}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/smsLogs/{id} [get]
func (a *SMSLog) Get(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	item, err := a.SMSLogBIZ.Get(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags 短信日志模块
// @Security ApiKeyAuth
// @Summary Update smsLog record by ID
// @Param id path string true "unique id"
// @Param body body schema.SMSLogForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/smsLogs/{id} [put]
func (a *SMSLog) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.SMSLogForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.SMSLogBIZ.Update(ctx, id, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 短信日志模块
// @Security ApiKeyAuth
// @Summary Delete smsLog record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/smsLogs/{id} [delete]
func (a *SMSLog) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.SMSLogBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
