package admin

import (
	"strconv"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"

	permissionService "github.com/break/junhong_cmp_fiber/internal/service/permission"
)

// PermissionHandler 权限 Handler
type PermissionHandler struct {
	service *permissionService.Service
}

// NewPermissionHandler 创建权限 Handler
func NewPermissionHandler(service *permissionService.Service) *PermissionHandler {
	return &PermissionHandler{service: service}
}

// Create 创建权限
// POST /api/admin/permissions
func (h *PermissionHandler) Create(c *fiber.Ctx) error {
	var req dto.CreatePermissionRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	permission, err := h.service.Create(c.UserContext(), &req)
	if err != nil {
		return err
	}

	return response.Success(c, permission)
}

// Get 获取权限详情
// GET /api/admin/permissions/:id
func (h *PermissionHandler) Get(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的权限 ID")
	}

	permission, err := h.service.Get(c.UserContext(), uint(id))
	if err != nil {
		return err
	}

	return response.Success(c, permission)
}

// Update 更新权限
// PUT /api/admin/permissions/:id
func (h *PermissionHandler) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的权限 ID")
	}

	var req dto.UpdatePermissionRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	permission, err := h.service.Update(c.UserContext(), uint(id), &req)
	if err != nil {
		return err
	}

	return response.Success(c, permission)
}

// Delete 删除权限
// DELETE /api/admin/permissions/:id
func (h *PermissionHandler) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的权限 ID")
	}

	if err := h.service.Delete(c.UserContext(), uint(id)); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// List 查询权限列表
// GET /api/admin/permissions
func (h *PermissionHandler) List(c *fiber.Ctx) error {
	var req dto.PermissionListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	permissions, total, err := h.service.List(c.UserContext(), &req)
	if err != nil {
		return err
	}

	return response.SuccessWithPagination(c, permissions, total, req.Page, req.PageSize)
}

// GetTree 获取权限树
// GET /api/admin/permissions/tree
func (h *PermissionHandler) GetTree(c *fiber.Ctx) error {
	var req dto.PermissionTreeRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	tree, err := h.service.GetTree(c.UserContext(), &req)
	if err != nil {
		return err
	}

	return response.Success(c, tree)
}
