package system

import (
	"kra/pkg/response"

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

type OperationRecordApi struct{}

// DeleteOperationRecordRequest 删除操作记录请求
type DeleteOperationRecordRequest struct {
	ID uint `json:"ID" binding:"required"`
}

// DeleteOperationRecordByIdsRequest 批量删除操作记录请求
type DeleteOperationRecordByIdsRequest struct {
	Ids []uint `json:"ids" binding:"required"`
}

// GetOperationRecordListRequest 获取操作记录列表请求
type GetOperationRecordListRequest struct {
	Page     int    `json:"page"`
	PageSize int    `json:"pageSize"`
	Method   string `json:"method"`
	Path     string `json:"path"`
	Status   int    `json:"status"`
}

// DeleteSysOperationRecord
// @Tags      SysOperationRecord
// @Summary   删除SysOperationRecord
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      DeleteOperationRecordRequest   true  "SysOperationRecord模型"
// @Success   200   {object}  response.Response{msg=string}  "删除SysOperationRecord"
// @Router    /sysOperationRecord/deleteSysOperationRecord [delete]
func (o *OperationRecordApi) DeleteSysOperationRecord(c *gin.Context) {
	var req DeleteOperationRecordRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := operationRecordUsecase.DeleteOperationRecord(c, req.ID); err != nil {
		response.FailWithMessage("删除失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

// DeleteSysOperationRecordByIds
// @Tags      SysOperationRecord
// @Summary   批量删除SysOperationRecord
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      DeleteOperationRecordByIdsRequest  true  "批量删除SysOperationRecord"
// @Success   200   {object}  response.Response{msg=string}      "批量删除SysOperationRecord"
// @Router    /sysOperationRecord/deleteSysOperationRecordByIds [delete]
func (o *OperationRecordApi) DeleteSysOperationRecordByIds(c *gin.Context) {
	var req DeleteOperationRecordByIdsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := operationRecordUsecase.DeleteOperationRecordByIds(c, req.Ids); err != nil {
		response.FailWithMessage("删除失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

// FindSysOperationRecord
// @Tags      SysOperationRecord
// @Summary   用id查询SysOperationRecord
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     ID    query     string                                                     true  "Id"
// @Success   200   {object}  response.Response{data=map[string]interface{},msg=string}  "用id查询SysOperationRecord"
// @Router    /sysOperationRecord/findSysOperationRecord [get]
func (o *OperationRecordApi) FindSysOperationRecord(c *gin.Context) {
	idStr := c.Query("ID")
	if idStr == "" {
		response.FailWithMessage("参数错误", c)
		return
	}

	var id uint
	// 简单转换
	for _, ch := range idStr {
		if ch >= '0' && ch <= '9' {
			id = id*10 + uint(ch-'0')
		}
	}

	record, err := operationRecordUsecase.GetOperationRecord(c, id)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(gin.H{"reSysOperationRecord": record}, "获取成功", c)
}

// GetSysOperationRecordList
// @Tags      SysOperationRecord
// @Summary   分页获取SysOperationRecord列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  query     GetOperationRecordListRequest                           true  "页码, 每页大小, 搜索条件"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取SysOperationRecord列表,返回包括列表,总数,页码,每页数量"
// @Router    /sysOperationRecord/getSysOperationRecordList [get]
func (o *OperationRecordApi) GetSysOperationRecordList(c *gin.Context) {
	var req GetOperationRecordListRequest
	// GET请求从query获取参数
	req.Page = 1
	req.PageSize = 10

	filters := make(map[string]interface{})
	if req.Method != "" {
		filters["method"] = req.Method
	}
	if req.Path != "" {
		filters["path"] = req.Path
	}
	if req.Status != 0 {
		filters["status"] = req.Status
	}

	list, total, err := operationRecordUsecase.GetOperationRecordList(c, req.Page, req.PageSize, filters)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}
