// Package admin 提供管理后台的 HTTP 处理器
// 包含账号管理、角色管理、权限管理、任务管理等功能的 Handler 实现
package admin

import (
	"strconv"

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

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	accountService "github.com/break/junhong_cmp_fiber/internal/service/account"
)

// AccountHandler 账号 Handler
type AccountHandler struct {
	service *accountService.Service
}

// NewAccountHandler 创建账号 Handler
func NewAccountHandler(service *accountService.Service) *AccountHandler {
	return &AccountHandler{service: service}
}

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

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

	return response.Success(c, account)
}

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

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

	return response.Success(c, account)
}

// Update 更新账号
// PUT /api/admin/accounts/:id
func (h *AccountHandler) 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.UpdateAccountRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

	return response.Success(c, account)
}

// BindWeCom 绑定账号与企业微信应用可见成员。
// PUT /api/admin/accounts/:id/wecom-binding
func (h *AccountHandler) BindWeCom(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
	}
	var request dto.BindAccountWeComRequest
	if err := c.BodyParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.service.BindWeCom(c.UserContext(), uint(id), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// Delete 删除账号
// DELETE /api/admin/accounts/:id
func (h *AccountHandler) 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/accounts
func (h *AccountHandler) List(c *fiber.Ctx) error {
	var req dto.AccountListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

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

// AssignRoles 为账号分配角色
// POST /api/admin/accounts/:id/roles
func (h *AccountHandler) AssignRoles(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.AssignRolesRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	ars, err := h.service.AssignRoles(c.UserContext(), uint(id), req.RoleIDs)
	if err != nil {
		return err
	}

	return response.Success(c, ars)
}

// GetRoles 获取账号的所有角色
// GET /api/admin/accounts/:id/roles
func (h *AccountHandler) GetRoles(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
	}

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

	return response.Success(c, roles)
}

// RemoveRole 移除账号的角色
// DELETE /api/admin/accounts/:account_id/roles/:role_id
func (h *AccountHandler) RemoveRole(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("account_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
	}

	roleID, err := strconv.ParseUint(c.Params("role_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的角色 ID")
	}

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

	return response.Success(c, nil)
}

// UpdatePassword 修改账号密码
// PUT /api/admin/accounts/:id/password
func (h *AccountHandler) UpdatePassword(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.UpdatePasswordRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

	return response.Success(c, nil)
}

// UpdateStatus 修改账号状态
// PUT /api/admin/accounts/:id/status
func (h *AccountHandler) UpdateStatus(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.UpdateStatusRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

	return response.Success(c, nil)
}

// ListPlatformAccounts 查询平台账号列表（兼容旧路由）
// 自动筛选 user_type IN (1, 2) 的账号
// GET /api/admin/accounts - 查询平台账号列表
func (h *AccountHandler) ListPlatformAccounts(c *fiber.Ctx) error {
	var req dto.PlatformAccountListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

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