package system

import (
	"strconv"

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

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

type DictionaryApi struct{}

// CreateDictionaryRequest 创建字典请求
type CreateDictionaryRequest struct {
	Name   string `json:"name" binding:"required"`
	Type   string `json:"type" binding:"required"`
	Status *bool  `json:"status"`
	Desc   string `json:"desc"`
}

// UpdateDictionaryRequest 更新字典请求
type UpdateDictionaryRequest struct {
	ID     uint   `json:"ID" binding:"required"`
	Name   string `json:"name"`
	Type   string `json:"type"`
	Status *bool  `json:"status"`
	Desc   string `json:"desc"`
}

// GetDictionaryListRequest 获取字典列表请求
type GetDictionaryListRequest struct {
	Page     int    `json:"page"`
	PageSize int    `json:"pageSize"`
	Name     string `json:"name"`
}

// CreateSysDictionary
// @Tags      SysDictionary
// @Summary   创建SysDictionary
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      CreateDictionaryRequest        true  "SysDictionary模型"
// @Success   200   {object}  response.Response{msg=string}  "创建SysDictionary"
// @Router    /sysDictionary/createSysDictionary [post]
func (d *DictionaryApi) CreateSysDictionary(c *gin.Context) {
	var req CreateDictionaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	dict := &system.Dictionary{
		Name:   req.Name,
		Type:   req.Type,
		Status: req.Status,
		Desc:   req.Desc,
	}

	if err := dictionaryUsecase.CreateDictionary(c, dict); err != nil {
		response.FailWithMessage("创建失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("创建成功", c)
}

// DeleteSysDictionary
// @Tags      SysDictionary
// @Summary   删除SysDictionary
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     ID    query     string                         true  "字典ID"
// @Success   200   {object}  response.Response{msg=string}  "删除SysDictionary"
// @Router    /sysDictionary/deleteSysDictionary [delete]
func (d *DictionaryApi) DeleteSysDictionary(c *gin.Context) {
	idStr := c.Query("ID")
	if idStr == "" {
		response.FailWithMessage("参数错误", c)
		return
	}

	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	if err := dictionaryUsecase.DeleteDictionary(c, uint(id)); err != nil {
		response.FailWithMessage("删除失败: "+err.Error(), c)
		return
	}

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

// UpdateSysDictionary
// @Tags      SysDictionary
// @Summary   更新SysDictionary
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      UpdateDictionaryRequest        true  "SysDictionary模型"
// @Success   200   {object}  response.Response{msg=string}  "更新SysDictionary"
// @Router    /sysDictionary/updateSysDictionary [put]
func (d *DictionaryApi) UpdateSysDictionary(c *gin.Context) {
	var req UpdateDictionaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	dict := &system.Dictionary{
		ID:     req.ID,
		Name:   req.Name,
		Type:   req.Type,
		Status: req.Status,
		Desc:   req.Desc,
	}

	if err := dictionaryUsecase.UpdateDictionary(c, dict); err != nil {
		response.FailWithMessage("更新失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindSysDictionary
// @Tags      SysDictionary
// @Summary   用id查询SysDictionary
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     ID    query     string                                                     false  "字典ID"
// @Param     type  query     string                                                     false  "字典英名"
// @Success   200   {object}  response.Response{data=map[string]interface{},msg=string}  "用id查询SysDictionary"
// @Router    /sysDictionary/findSysDictionary [get]
func (d *DictionaryApi) FindSysDictionary(c *gin.Context) {
	idStr := c.Query("ID")
	dictType := c.Query("type")

	var id uint
	if idStr != "" {
		idUint, err := strconv.ParseUint(idStr, 10, 64)
		if err != nil {
			response.FailWithMessage("参数错误", c)
			return
		}
		id = uint(idUint)
	}

	dict, err := dictionaryUsecase.GetDictionary(c, dictType, id, nil)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

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

// GetSysDictionaryList
// @Tags      SysDictionary
// @Summary   分页获取SysDictionary列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     name  query     string                                                     false  "字典名称"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}     "分页获取SysDictionary列表,返回包括列表,总数,页码,每页数量"
// @Router    /sysDictionary/getSysDictionaryList [get]
func (d *DictionaryApi) GetSysDictionaryList(c *gin.Context) {
	name := c.Query("name")

	list, err := dictionaryUsecase.GetDictionaryList(c, name)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

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

// ExportSysDictionary
// @Tags      SysDictionary
// @Summary   导出字典JSON（包含字典详情）
// @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}  "导出字典JSON"
// @Router    /sysDictionary/exportSysDictionary [get]
func (d *DictionaryApi) ExportSysDictionary(c *gin.Context) {
	idStr := c.Query("ID")
	if idStr == "" {
		response.FailWithMessage("字典ID不能为空", c)
		return
	}

	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	exportData, err := dictionaryUsecase.ExportDictionary(c, uint(id))
	if err != nil {
		response.FailWithMessage("导出失败: "+err.Error(), c)
		return
	}

	response.OkWithDetailed(exportData, "导出成功", c)
}

// ImportSysDictionaryRequest 导入字典请求
type ImportSysDictionaryRequest struct {
	Json string `json:"json" binding:"required"`
}

// ImportSysDictionary
// @Tags      SysDictionary
// @Summary   导入字典JSON（包含字典详情）
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      ImportSysDictionaryRequest     true  "字典JSON数据"
// @Success   200   {object}  response.Response{msg=string}  "导入字典"
// @Router    /sysDictionary/importSysDictionary [post]
func (d *DictionaryApi) ImportSysDictionary(c *gin.Context) {
	var req ImportSysDictionaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := dictionaryUsecase.ImportDictionary(c, req.Json); err != nil {
		response.FailWithMessage("导入失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("导入成功", c)
}
