package routes

import (
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/handler/admin"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/middleware"
	"github.com/break/junhong_cmp_fiber/pkg/openapi"
)

// registerRefundRoutes 注册退款管理路由
// 平台和代理账号可访问，企业账号禁止访问
func registerRefundRoutes(router fiber.Router, handler *admin.RefundHandler, doc *openapi.Generator, basePath string) {
	refund := router.Group("/refunds", func(c *fiber.Ctx) error {
		userType := middleware.GetUserTypeFromContext(c.UserContext())
		if userType != constants.UserTypeSuperAdmin &&
			userType != constants.UserTypePlatform &&
			userType != constants.UserTypeAgent {
			return errors.New(errors.CodeForbidden, "无权限访问退款管理功能")
		}
		return c.Next()
	})
	groupPath := basePath + "/refunds"

	Register(refund, doc, groupPath, "POST", "", handler.Create, RouteSpec{
		Summary: "创建退款申请",
		Tags:    []string{"退款管理"},
		Input:   new(dto.CreateRefundRequest),
		Output:  new(dto.RefundResponse),
		Auth:    true,
	})

	Register(refund, doc, groupPath, "GET", "", handler.List, RouteSpec{
		Summary: "退款申请列表",
		Tags:    []string{"退款管理"},
		Input:   new(dto.RefundListRequest),
		Output:  new(dto.RefundListResponse),
		Auth:    true,
	})

	Register(refund, doc, groupPath, "GET", "/:id", handler.GetByID, RouteSpec{
		Summary: "退款申请详情",
		Tags:    []string{"退款管理"},
		Input:   new(dto.RefundIDRequest),
		Output:  new(dto.RefundResponse),
		Auth:    true,
	})

	Register(refund, doc, groupPath, "POST", "/:id/approve", handler.Approve, RouteSpec{
		Summary: "审批通过退款申请",
		Tags:    []string{"退款管理"},
		Input:   new(dto.ApproveRefundRequest),
		Output:  nil,
		Auth:    true,
	})

	Register(refund, doc, groupPath, "POST", "/:id/reject", handler.Reject, RouteSpec{
		Summary: "审批拒绝退款申请",
		Tags:    []string{"退款管理"},
		Input:   new(dto.RejectRefundRequest),
		Output:  nil,
		Auth:    true,
	})

	Register(refund, doc, groupPath, "POST", "/:id/return", handler.Return, RouteSpec{
		Summary: "退回退款申请",
		Tags:    []string{"退款管理"},
		Input:   new(dto.ReturnRefundRequest),
		Output:  nil,
		Auth:    true,
	})

	Register(refund, doc, groupPath, "POST", "/:id/resubmit", handler.Resubmit, RouteSpec{
		Summary: "重新提交退款申请",
		Tags:    []string{"退款管理"},
		Input:   new(dto.ResubmitRefundRequest),
		Output:  nil,
		Auth:    true,
	})
}
