package model

import (
	"time"

	"gorm.io/gorm"
)

// RefundRequest 退款申请模型
// 记录退款申请的完整生命周期，包含退款单号、关联订单、金额、状态流转、审批信息等
// 状态流转：1(待审批) → 2(已通过)/3(已拒绝)/4(已退回)，4(已退回) → 1(待审批)
type RefundRequest struct {
	gorm.Model
	BaseModel `gorm:"embedded"`

	// 退款单基础信息
	RefundNo        string `gorm:"column:refund_no;type:varchar(50);uniqueIndex:idx_refund_request_refund_no,where:deleted_at IS NULL;not null;comment:退款单号（RF+日期时间+6位随机数）" json:"refund_no"`
	OrderID         uint   `gorm:"column:order_id;index;not null;comment:关联订单ID" json:"order_id"`
	OrderNo         string `gorm:"column:order_no;type:varchar(30);not null;default:'';comment:关联订单号快照" json:"order_no"`
	OrderType       string `gorm:"column:order_type;type:varchar(20);not null;default:'';comment:订单类型快照（single_card/device）" json:"order_type,omitempty"`
	AssetIdentifier string `gorm:"column:asset_identifier;type:varchar(100);not null;default:'';comment:下单时资产标识符快照（卡为ICCID，设备优先VirtualNo、其次IMEI）" json:"asset_identifier,omitempty"`
	IotCardID       *uint  `gorm:"column:iot_card_id;comment:下单时IoT卡ID快照" json:"iot_card_id,omitempty"`
	DeviceID        *uint  `gorm:"column:device_id;comment:下单时设备ID快照" json:"device_id,omitempty"`
	PackageUsageID  *uint  `gorm:"column:package_usage_id;comment:关联套餐使用记录ID（可选）" json:"package_usage_id,omitempty"`
	ShopID          *uint  `gorm:"column:shop_id;index;comment:店铺ID（从订单获取，用于数据权限过滤）" json:"shop_id,omitempty"`
	ShopName        string `gorm:"column:shop_name;->" json:"shop_name,omitempty"`

	// 金额信息
	ActualReceivedAmount  int64  `gorm:"column:actual_received_amount;type:bigint;not null;comment:实收金额（分）" json:"actual_received_amount"`
	RequestedRefundAmount int64  `gorm:"column:requested_refund_amount;type:bigint;not null;comment:申请退款金额（分）" json:"requested_refund_amount"`
	ApprovedRefundAmount  *int64 `gorm:"column:approved_refund_amount;type:bigint;comment:审批实际退款金额（分）" json:"approved_refund_amount,omitempty"`

	// 退款凭证、原因和状态
	RefundVoucherKey StringJSONBArray `gorm:"column:refund_voucher_key;type:jsonb;not null;comment:退款凭证对象存储file_key列表（jsonb存储[]string）" json:"refund_voucher_key"`
	RefundReason     string           `gorm:"column:refund_reason;type:text;comment:退款原因" json:"refund_reason"`
	Status           int              `gorm:"column:status;type:int;not null;default:1;index;comment:状态 1-待审批 2-已通过 3-已拒绝 4-已退回" json:"status"`

	// 审批信息
	ApprovalInstanceID *uint      `gorm:"column:approval_instance_id;comment:关联的唯一通用审批实例ID" json:"approval_instance_id,omitempty"`
	ProcessorID        *uint      `gorm:"column:processor_id;comment:审批人ID" json:"processor_id,omitempty"`
	ProcessedAt        *time.Time `gorm:"column:processed_at;comment:审批时间" json:"processed_at,omitempty"`
	RejectReason       string     `gorm:"column:reject_reason;type:text;comment:拒绝原因" json:"reject_reason,omitempty"`
	Remark             string     `gorm:"column:remark;type:text;comment:审批备注" json:"remark,omitempty"`

	// 后处理标记
	CommissionDeducted bool `gorm:"column:commission_deducted;not null;default:false;comment:佣金是否已回扣" json:"commission_deducted"`
	AssetReset         bool `gorm:"column:asset_reset;not null;default:false;comment:退款后资产处理是否完成" json:"asset_reset"`
}

// TableName 指定表名
func (RefundRequest) TableName() string {
	return "tb_refund_request"
}

// 退款状态常量
const (
	RefundStatusPending  = 1 // 待审批
	RefundStatusApproved = 2 // 已通过
	RefundStatusRejected = 3 // 已拒绝
	RefundStatusReturned = 4 // 已退回
)
