package example

import (
	"kra/internal/biz/example"
	"kra/pkg/response"
	"kra/pkg/utils"

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

type CustomerApi struct{}

// CreateExaCustomer
// @Tags      ExaCustomer
// @Summary   创建客户
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      CustomerRequest                true  "客户用户名, 客户手机号码"
// @Success   200   {object}  response.Response{msg=string}  "创建客户"
// @Router    /customer/customer [post]
func (api *CustomerApi) CreateExaCustomer(c *gin.Context) {
	var req CustomerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	customer := &example.Customer{
		CustomerName:       req.CustomerName,
		CustomerPhoneData:  req.CustomerPhoneData,
		SysUserID:          utils.GetUserID(c),
		SysUserAuthorityID: utils.GetUserAuthorityId(c),
	}

	if err := customerUsecase.CreateExaCustomer(c.Request.Context(), customer); err != nil {
		response.FailWithMessage("创建失败: "+err.Error(), c)
		return
	}

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

// DeleteExaCustomer
// @Tags      ExaCustomer
// @Summary   删除客户
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      DeleteCustomerRequest          true  "客户ID"
// @Success   200   {object}  response.Response{msg=string}  "删除客户"
// @Router    /customer/customer [delete]
func (api *CustomerApi) DeleteExaCustomer(c *gin.Context) {
	var req DeleteCustomerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

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

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

// UpdateExaCustomer
// @Tags      ExaCustomer
// @Summary   更新客户信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      CustomerRequest                true  "客户ID, 客户信息"
// @Success   200   {object}  response.Response{msg=string}  "更新客户信息"
// @Router    /customer/customer [put]
func (api *CustomerApi) UpdateExaCustomer(c *gin.Context) {
	var req CustomerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	customer := &example.Customer{
		ID:                req.ID,
		CustomerName:      req.CustomerName,
		CustomerPhoneData: req.CustomerPhoneData,
	}

	if err := customerUsecase.UpdateExaCustomer(c.Request.Context(), customer); err != nil {
		response.FailWithMessage("更新失败: "+err.Error(), c)
		return
	}

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

// GetExaCustomer
// @Tags      ExaCustomer
// @Summary   获取单一客户信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  query     GetCustomerRequest                                             true  "客户ID"
// @Success   200   {object}  response.Response{data=CustomerResponse,msg=string}            "获取单一客户信息,返回包括客户详情"
// @Router    /customer/customer [get]
func (api *CustomerApi) GetExaCustomer(c *gin.Context) {
	var req GetCustomerRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	customer, err := customerUsecase.GetExaCustomer(c.Request.Context(), req.ID)
	if err != nil {
		response.FailWithMessage("获取失败: "+err.Error(), c)
		return
	}

	response.OkWithDetailed(toCustomerResponse(customer), "获取成功", c)
}

// GetExaCustomerList
// @Tags      ExaCustomer
// @Summary   分页获取权限客户列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  query     GetCustomerListRequest                                  true  "页码, 每页大小"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取权限客户列表,返回包括列表,总数,页码,每页数量"
// @Router    /customer/customerList [get]
func (api *CustomerApi) GetExaCustomerList(c *gin.Context) {
	var req GetCustomerListRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	authorityId := utils.GetUserAuthorityId(c)
	list, total, err := customerUsecase.GetCustomerInfoList(c.Request.Context(), authorityId, req.Page, req.PageSize)
	if err != nil {
		response.FailWithMessage("获取失败: "+err.Error(), c)
		return
	}

	respList := make([]CustomerResponse, len(list))
	for i, cust := range list {
		respList[i] = *toCustomerResponse(cust)
	}

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

// Request/Response 结构
type CustomerRequest struct {
	ID                uint   `json:"ID"`
	CustomerName      string `json:"customerName"`
	CustomerPhoneData string `json:"customerPhoneData"`
}

type DeleteCustomerRequest struct {
	ID uint `json:"ID"`
}

type GetCustomerRequest struct {
	ID uint `form:"ID"`
}

type GetCustomerListRequest struct {
	Page     int `form:"page"`
	PageSize int `form:"pageSize"`
}

type CustomerResponse struct {
	ID                 uint   `json:"ID"`
	CustomerName       string `json:"customerName"`
	CustomerPhoneData  string `json:"customerPhoneData"`
	SysUserID          uint   `json:"sysUserId"`
	SysUserAuthorityID uint   `json:"sysUserAuthorityID"`
	SysUserName        string `json:"sysUserName"`
}

func toCustomerResponse(c *example.Customer) *CustomerResponse {
	return &CustomerResponse{
		ID:                 c.ID,
		CustomerName:       c.CustomerName,
		CustomerPhoneData:  c.CustomerPhoneData,
		SysUserID:          c.SysUserID,
		SysUserAuthorityID: c.SysUserAuthorityID,
		SysUserName:        c.SysUserName,
	}
}
