package model

import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

// CommissionWithdrawalRequest 佣金提现申请模型
// 代理佣金提现申请、审批流程、提现记录查询
type CommissionWithdrawalRequest struct {
	gorm.Model
	BaseModel        `gorm:"embedded"`
	WithdrawalNo     string         `gorm:"column:withdrawal_no;type:varchar(50);uniqueIndex:uk_commission_withdrawal_no,where:deleted_at IS NULL AND withdrawal_no IS NOT NULL;comment:提现单号（唯一，格式：W + 时间戳 + 随机数）" json:"withdrawal_no"`
	AgentID          uint           `gorm:"column:agent_id;index;not null;comment:代理用户ID" json:"agent_id"`
	ApplicantID      uint           `gorm:"column:applicant_id;index;comment:申请人账号ID" json:"applicant_id"`
	ShopID           uint           `gorm:"column:shop_id;index;comment:店铺ID（冗余字段）" json:"shop_id"`
	Amount           int64          `gorm:"column:amount;type:bigint;not null;comment:提现金额(分为单位)" json:"amount"`
	Fee              int64          `gorm:"column:fee;type:bigint;default:0;comment:手续费(分为单位)" json:"fee"`
	FeeRate          int64          `gorm:"column:fee_rate;type:bigint;default:0;comment:手续费比率（基点，100=1%，快照）" json:"fee_rate"`
	ActualAmount     int64          `gorm:"column:actual_amount;type:bigint;comment:实际到账金额(分为单位)" json:"actual_amount"`
	WithdrawalMethod string         `gorm:"column:withdrawal_method;type:varchar(20);comment:提现方式 alipay-支付宝 wechat-微信 bank-银行卡" json:"withdrawal_method"`
	PaymentType      string         `gorm:"column:payment_type;type:varchar(20);default:'manual';comment:放款类型（manual=人工打款）" json:"payment_type"`
	AccountInfo      datatypes.JSON `gorm:"column:account_info;type:jsonb;comment:收款账户信息(姓名、账号等)" json:"account_info"`
	Status           int            `gorm:"column:status;type:int;default:1;comment:状态 1-待审核 2-已通过 3-已拒绝 4-已到账" json:"status"`
	ProcessorID      uint           `gorm:"column:processor_id;index;comment:处理人ID" json:"processor_id"`
	ProcessedAt      *time.Time     `gorm:"column:processed_at;comment:处理时间" json:"processed_at"`
	PaidAt           *time.Time     `gorm:"column:paid_at;comment:到账时间" json:"paid_at"`
	RejectReason     string         `gorm:"column:reject_reason;type:text;comment:拒绝原因" json:"reject_reason"`
	Remark           string         `gorm:"column:remark;type:text;comment:备注" json:"remark"`
}

// TableName 指定表名
func (CommissionWithdrawalRequest) TableName() string {
	return "tb_commission_withdrawal_request"
}

// CommissionWithdrawalSetting 佣金提现设置模型
// 提现参数配置(最低金额、手续费率、到账时间等)
type CommissionWithdrawalSetting struct {
	gorm.Model
	BaseModel            `gorm:"embedded"`
	MinWithdrawalAmount  int64 `gorm:"column:min_withdrawal_amount;type:bigint;comment:最低提现金额(分为单位)" json:"min_withdrawal_amount"`
	FeeRate              int64 `gorm:"column:fee_rate;type:bigint;comment:手续费率(万分比,如100表示1%)" json:"fee_rate"`
	ArrivalDays          int   `gorm:"column:arrival_days;type:int;comment:到账天数" json:"arrival_days"`
	DailyWithdrawalLimit int   `gorm:"column:daily_withdrawal_limit;type:int;default:3;comment:每日提现次数限制" json:"daily_withdrawal_limit"`
	IsActive             bool  `gorm:"column:is_active;type:boolean;default:true;comment:是否生效(最新一条)" json:"is_active"`
}

// TableName 指定表名
func (CommissionWithdrawalSetting) TableName() string {
	return "tb_commission_withdrawal_setting"
}
