package model

import (
	"database/sql/driver"
	"encoding/json"
	"time"
)

// PaymentMerchantConfig 支付商户配置
type PaymentMerchantConfig struct {
	ID           uint64    `gorm:"primaryKey" json:"id"`
	Name         string    `gorm:"size:128;not null" json:"name"`
	Provider     string    `gorm:"size:32;not null;index:idx_payment_merchant_configs_provider" json:"provider"`
	MerchantID   string    `gorm:"size:128;not null;index:idx_payment_merchant_configs_merchant_id" json:"merchant_id"`
	GatewayURL   string    `gorm:"size:512;not null;default:''" json:"gateway_url"`
	SignKey      string    `gorm:"type:text" json:"sign_key"`   // 加密存储，拉卡拉使用商户私钥 PEM
	NotifyKey    string    `gorm:"type:text" json:"notify_key"` // 加密存储，拉卡拉使用通知验签证书 PEM
	NotifyURL    string    `gorm:"size:512;not null;default:''" json:"notify_url"`
	JumpURL      string    `gorm:"size:512;not null;default:''" json:"jump_url"`
	PayWay       string    `gorm:"size:32;not null;default:'ZFBZF';index:idx_payment_merchant_configs_pay_way" json:"pay_way"`
	JSPayFlag    string    `gorm:"column:jspay_flag;size:8;not null;default:'2'" json:"jspay_flag"`
	SignType     string    `gorm:"size:32;not null;default:'MD5'" json:"sign_type"`
	ExtraConfig  JSONMap   `gorm:"type:json" json:"extra_config"`
	IsDefault    bool      `gorm:"not null;default:0;index:idx_payment_merchant_configs_default" json:"is_default"`
	Status       string    `gorm:"size:32;not null;default:'active';index:idx_payment_merchant_configs_status" json:"status"`
	Environment  string    `gorm:"size:16;not null;default:'production'" json:"environment"`
	BusinessTags JSONArray `gorm:"type:json" json:"business_tags"`

	// 统计信息
	TotalTransactions int64      `gorm:"not null;default:0" json:"total_transactions"`
	TotalAmountCent   int64      `gorm:"not null;default:0" json:"total_amount_cent"`
	LastUsedAt        *time.Time `json:"last_used_at"`

	// 审计信息
	CreatedBy *uint64   `json:"created_by"`
	UpdatedBy *uint64   `json:"updated_by"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func (PaymentMerchantConfig) TableName() string {
	return "payment_merchant_configs"
}

// JSONMap 用于 extra_config 字段
type JSONMap map[string]any

func (j JSONMap) Value() (driver.Value, error) {
	if j == nil {
		return nil, nil
	}
	return json.Marshal(j)
}

func (j *JSONMap) Scan(value any) error {
	if value == nil {
		*j = nil
		return nil
	}
	bytes, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(bytes, j)
}

// JSONArray 用于 business_tags 字段
type JSONArray []string

func (j JSONArray) Value() (driver.Value, error) {
	if j == nil {
		return nil, nil
	}
	return json.Marshal(j)
}

func (j *JSONArray) Scan(value any) error {
	if value == nil {
		*j = nil
		return nil
	}
	bytes, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(bytes, j)
}

// PaymentConfigUsageLog 支付配置使用日志
type PaymentConfigUsageLog struct {
	ID             uint64    `gorm:"primaryKey" json:"id"`
	ConfigID       uint64    `gorm:"not null;index:idx_payment_config_usage_logs_config" json:"config_id"`
	PaymentOrderID uint64    `gorm:"not null;index:idx_payment_config_usage_logs_payment" json:"payment_order_id"`
	Provider       string    `gorm:"size:32;not null" json:"provider"`
	MerchantID     string    `gorm:"size:128;not null" json:"merchant_id"`
	AmountCent     int64     `gorm:"not null" json:"amount_cent"`
	BizType        string    `gorm:"size:32;not null" json:"biz_type"`
	CreatedAt      time.Time `json:"created_at"`
}

func (PaymentConfigUsageLog) TableName() string {
	return "payment_config_usage_logs"
}
