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/openapi"
)

func registerAssetRoutes(router fiber.Router, handler *admin.AssetHandler, walletHandler *admin.AssetWalletHandler, doc *openapi.Generator, basePath string) {
	assets := router.Group("/assets")
	groupPath := basePath + "/assets"

	Register(assets, doc, groupPath, "GET", "/resolve/:identifier", handler.Resolve, RouteSpec{
		Summary:     "解析资产",
		Description: "通过虚拟号/ICCID/IMEI/SN/MSISDN 解析设备或卡的完整详情。exchange_trace 始终存在，previous_asset/next_asset 无关系时为 null；关联项仅在 can_view=true 且 asset_id 非空时允许跳转。企业账号禁止调用。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetResolveRequest),
		Output:      new(dto.AssetResolveResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/realtime-status", handler.RealtimeStatus, RouteSpec{
		Summary:     "资产实时状态",
		Description: "查询资产实时状态。卡类型：读取 DB/Redis 持久化状态；设备类型：实时调用 Gateway sync-info 接口获取最新数据，Gateway 失败不阻断主流程（DeviceRealtime.gateway_msg 返回失败原因）。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      new(dto.AssetRealtimeStatusResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "POST", "/:identifier/refresh", handler.Refresh, RouteSpec{
		Summary:     "刷新资产状态",
		Description: "主动调网关同步最新状态。设备有30秒冷却期。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      new(dto.AssetRealtimeStatusResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/packages", handler.Packages, RouteSpec{
		Summary:     "资产套餐列表",
		Description: "查询该资产所有套餐记录，含虚流量换算结果。支持分页与 status 状态筛选。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetPackagesRequest),
		Output:      new(dto.AssetPackagesResult),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/current-package", handler.CurrentPackage, RouteSpec{
		Summary: "当前生效套餐",
		Tags:    []string{"资产管理"},
		Input:   new(dto.AssetIdentifierRequest),
		Output:  new(dto.AssetPackageResponse),
		Auth:    true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/packages/:package_usage_id/expires-at", handler.UpdatePackageExpiresAt, RouteSpec{
		Summary:     "修改资产套餐过期时间",
		Description: "修改指定资产下某条套餐使用记录的过期时间。仅账号ID为2的账号可调用。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.UpdateAssetPackageExpiresAtRequest),
		Output:      new(dto.AssetPackageResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/packages/:package_usage_id/used-data", handler.UpdatePackageUsage, RouteSpec{
		Summary:     "修改资产套餐已用量",
		Description: "修改指定资产下某条套餐使用记录的真实已用量(MB)。仅账号ID为2的账号可调用。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.UpdateAssetPackageUsageRequest),
		Output:      new(dto.AssetPackageResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "POST", "/:identifier/stop", handler.Stop, RouteSpec{
		Summary:     "停机",
		Description: "设备批量停机设备下所有已实名卡；单卡手动停机。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      nil,
		Auth:        true,
	})

	Register(assets, doc, groupPath, "POST", "/:identifier/start", handler.Start, RouteSpec{
		Summary:     "复机",
		Description: "设备批量复机；单卡手动复机。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      nil,
		Auth:        true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/deactivate", handler.Deactivate, RouteSpec{
		Summary:     "停用资产",
		Description: "将资产状态设置为停用（不可逆）。仅支持库存中或已售出的资产。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      nil,
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/wallet", walletHandler.GetWallet, RouteSpec{
		Summary:     "资产钱包概况",
		Description: "查询指定卡或设备的钱包余额概况。企业账号禁止调用。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetIdentifierRequest),
		Output:      new(dto.AssetWalletResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/wallet/transactions", walletHandler.ListTransactions, RouteSpec{
		Summary:     "资产钱包流水列表",
		Description: "分页查询指定资产的钱包收支流水，含充值/扣款来源编号。企业账号禁止调用。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetWalletTransactionListRequest),
		Output:      new(dto.AssetWalletTransactionListResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/orders", handler.Orders, RouteSpec{
		Summary:     "资产历史订单",
		Description: "查询资产本代历史订单，支持 include_previous=true 参数通过换货链追溯前代订单（最多10代）。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetOrdersRequest),
		Output:      new(dto.AssetOrdersResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "GET", "/:identifier/operation-logs", handler.OperationLogs, RouteSpec{
		Summary:     "资产操作审计日志",
		Description: "通过资产标识符查询审计日志，支持分页和操作类型/结果状态筛选。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.AssetOperationLogListRequest),
		Output:      new(dto.AssetOperationLogListResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/polling-status", handler.UpdatePollingStatus, RouteSpec{
		Summary:     "更新资产轮询状态",
		Description: "启用或禁用指定资产（IoT卡或设备）的定期状态轮询。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.UpdateAssetPollingStatusRequest),
		Output:      new(dto.UpdateAssetPollingStatusResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/realname-mode", handler.UpdateRealnamePolicy, RouteSpec{
		Summary:     "更新资产实名认证策略",
		Description: "更新指定资产（IoT卡或设备）的实名认证策略，支持 none/before_order/after_order 三种模式。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.UpdateAssetRealnamePolicyRequest),
		Output:      new(dto.UpdateAssetRealnamePolicyResponse),
		Auth:        true,
	})

	Register(assets, doc, groupPath, "PATCH", "/:identifier/realname-status", handler.UpdateRealnameStatus, RouteSpec{
		Summary:     "手动更新卡实名状态",
		Description: "手动将指定卡资产标记为已实名或未实名。状态更新后会触发停复机评估，并在标记为已实名时触发待实名套餐激活。",
		Tags:        []string{"资产管理"},
		Input:       new(dto.UpdateAssetRealnameStatusRequest),
		Output:      new(dto.UpdateAssetRealnameStatusResponse),
		Auth:        true,
	})
}
