package system

import (
	"kra/internal/biz/system"
	"kra/pkg/response"

	"github.com/gin-gonic/gin"
)

type AutoCodeHistoryApi struct{}

// GetByIdReq 获取请求
type GetByIdReq struct {
	ID uint `json:"id" form:"id"`
}

// First 获取meta信息
// @Summary 获取meta信息
// @Tags AutoCode
// @Accept json
// @Produce json
// @Param data body GetByIdReq true "请求参数"
// @Success 200 {object} response.Response{data=map[string]interface{}}
// @Router /autoCode/getMeta [post]
func (api *AutoCodeHistoryApi) First(c *gin.Context) {
	var req GetByIdReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	data, err := autoCodeHistoryUsecase.First(c.Request.Context(), req.ID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithDetailed(gin.H{"meta": data}, "获取成功", c)
}

// Delete 删除回滚记录
// @Summary 删除回滚记录
// @Tags AutoCode
// @Accept json
// @Produce json
// @Param data body GetByIdReq true "请求参数"
// @Success 200 {object} response.Response
// @Router /autoCode/delSysHistory [post]
func (api *AutoCodeHistoryApi) Delete(c *gin.Context) {
	var req GetByIdReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := autoCodeHistoryUsecase.Delete(c.Request.Context(), req.ID)
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// RollBack 回滚自动生成代码
// @Summary 回滚自动生成代码
// @Tags AutoCode
// @Accept json
// @Produce json
// @Param data body system.SysAutoHistoryRollBack true "请求参数"
// @Success 200 {object} response.Response
// @Router /autoCode/rollback [post]
func (api *AutoCodeHistoryApi) RollBack(c *gin.Context) {
	var req system.SysAutoHistoryRollBack
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := autoCodeHistoryUsecase.RollBack(c.Request.Context(), req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("回滚成功", c)
}

// PageInfoReq 分页请求
type PageInfoReq struct {
	Page     int `json:"page" form:"page"`
	PageSize int `json:"pageSize" form:"pageSize"`
}

// GetList 查询回滚记录
// @Summary 查询回滚记录
// @Tags AutoCode
// @Accept json
// @Produce json
// @Param data body PageInfoReq true "请求参数"
// @Success 200 {object} response.Response{data=response.PageResult}
// @Router /autoCode/getSysHistory [post]
func (api *AutoCodeHistoryApi) GetList(c *gin.Context) {
	var req PageInfoReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := autoCodeHistoryUsecase.GetList(c.Request.Context(), req.Page, req.PageSize)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}
