package admin

import (
	"strconv"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"

	wecomapp "github.com/break/junhong_cmp_fiber/internal/application/wecom"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// WeComHandler 提供企业微信自建应用连接配置接口。
type WeComHandler struct {
	service   *wecomapp.ConnectionService
	directory *wecomapp.DirectoryService
	scenes    *wecomapp.SceneService
	validator *validator.Validate
}

// SetSceneService 注入企业微信审批场景配置用例。
func (h *WeComHandler) SetSceneService(service *wecomapp.SceneService) {
	h.scenes = service
}

// NewWeComHandler 创建企业微信连接配置 Handler。
func NewWeComHandler(service *wecomapp.ConnectionService, validate *validator.Validate) *WeComHandler {
	return &WeComHandler{service: service, validator: validate}
}

// SetDirectoryService 注入企业微信通讯录同步用例。
func (h *WeComHandler) SetDirectoryService(service *wecomapp.DirectoryService) {
	h.directory = service
}

// Save 创建或更新企业微信应用连接配置。
// POST /api/admin/wecom/applications
func (h *WeComHandler) Save(c *fiber.Ctx) error {
	if h == nil || h.service == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信连接服务未配置")
	}
	var request dto.SaveWeComApplicationRequest
	if err := c.BodyParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.service.Save(c.UserContext(), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// List 查询企业微信应用连接配置。
// GET /api/admin/wecom/applications
func (h *WeComHandler) List(c *fiber.Ctx) error {
	if h == nil || h.service == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信连接服务未配置")
	}
	var request dto.WeComApplicationListRequest
	if err := c.QueryParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.service.List(c.UserContext(), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// Test 测试指定企业微信应用能否成功取得 access_token。
// POST /api/admin/wecom/applications/:id/test
func (h *WeComHandler) Test(c *fiber.Ctx) error {
	if h == nil || h.service == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信连接服务未配置")
	}
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "企业微信应用配置 ID 无效")
	}
	if err := h.service.Test(c.UserContext(), uint(id)); err != nil {
		return err
	}
	return response.Success(c, dto.WeComConnectionTestResponse{Success: true})
}

// SaveDefaultCreator 保存应用默认审批发起人。
// PUT /api/admin/wecom/applications/:id/default-creator
func (h *WeComHandler) SaveDefaultCreator(c *fiber.Ctx) error {
	if h == nil || h.service == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信连接服务未配置")
	}
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "企业微信应用配置 ID 无效")
	}
	var request dto.SaveWeComDefaultCreatorRequest
	if err := c.BodyParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.service.SaveDefaultCreator(c.UserContext(), uint(id), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// SyncMembers 同步指定应用当前可见成员。
// POST /api/admin/wecom/applications/:id/members/sync
func (h *WeComHandler) SyncMembers(c *fiber.Ctx) error {
	if h == nil || h.directory == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信通讯录服务未配置")
	}
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "企业微信应用配置 ID 无效")
	}
	result, err := h.directory.Sync(c.UserContext(), uint(id))
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// ListMembers 分页查询指定应用最近同步的可见成员。
// GET /api/admin/wecom/applications/:id/members
func (h *WeComHandler) ListMembers(c *fiber.Ctx) error {
	if h == nil || h.directory == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信通讯录服务未配置")
	}
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "企业微信应用配置 ID 无效")
	}
	var request dto.WeComMemberListRequest
	if err := c.QueryParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.directory.List(c.UserContext(), uint(id), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// SaveScene 保存并校验稳定业务类型的企微模板控件映射。
// PUT /api/admin/wecom/scenes/:business_type
func (h *WeComHandler) SaveScene(c *fiber.Ctx) error {
	if h == nil || h.scenes == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信审批场景服务未配置")
	}
	var request dto.SaveWeComApprovalSceneRequest
	if err := c.BodyParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.scenes.Save(c.UserContext(), c.Params("business_type"), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// ListScenes 分页查询企业微信审批场景当前配置。
// GET /api/admin/wecom/scenes
func (h *WeComHandler) ListScenes(c *fiber.Ctx) error {
	if h == nil || h.scenes == nil || h.validator == nil {
		return errors.New(errors.CodeServiceUnavailable, "企业微信审批场景服务未配置")
	}
	var request dto.WeComApprovalSceneListRequest
	if err := c.QueryParser(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&request); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.scenes.List(c.UserContext(), request)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}
