package paymentconfig

import "errors"

var (
	ErrConfigNotFound      = errors.New("payment config not found")
	ErrDuplicateDefault    = errors.New("duplicate default config for provider")
	ErrInvalidProvider     = errors.New("invalid payment provider")
	ErrInvalidPayWay       = errors.New("invalid pay_way")
	ErrInvalidStatus       = errors.New("invalid status")
	ErrInvalidBackup       = errors.New("invalid payment config backup")
	ErrEmptyBackup         = errors.New("payment config backup is empty")
	ErrCannotDeleteInUse   = errors.New("cannot delete config in use")
	ErrEncryptionFailed    = errors.New("encryption failed")
	ErrDecryptionFailed    = errors.New("decryption failed")
	ErrNoActiveConfigFound = errors.New("no active config found for provider")
	ErrMerchantIDRequired  = errors.New("merchant_id is required")
	ErrNameRequired        = errors.New("name is required")
	ErrGatewayURLRequired  = errors.New("gateway_url is required for leshua provider")
	ErrSignKeyRequired     = errors.New("sign_key is required for leshua provider")
	ErrNotifyKeyRequired   = errors.New("notify_key is required for leshua provider")
	ErrNotifyURLRequired   = errors.New("notify_url is required for leshua provider")
	ErrInvalidSignType     = errors.New("invalid sign_type")
	ErrAppIDRequired       = errors.New("extra_config.app_id is required for lakala provider")
	ErrSerialNoRequired    = errors.New("extra_config.serial_no is required for lakala provider")
	ErrTermNoRequired      = errors.New("extra_config.term_no is required for lakala provider")
	ErrSecretIDRequired    = errors.New("extra_config.secret_id is required for shuncheng provider")
)

// DTO 数据传输对象
type ConfigDTO struct {
	ID                uint64         `json:"id"`
	Name              string         `json:"name"`
	Provider          string         `json:"provider"`
	MerchantID        string         `json:"merchant_id"`
	GatewayURL        string         `json:"gateway_url"`
	SignKey           string         `json:"sign_key,omitempty"`   // 默认不返回
	NotifyKey         string         `json:"notify_key,omitempty"` // 默认不返回
	NotifyURL         string         `json:"notify_url"`
	JumpURL           string         `json:"jump_url"`
	PayWay            string         `json:"pay_way"`
	JSPayFlag         string         `json:"jspay_flag"`
	SignType          string         `json:"sign_type"`
	ExtraConfig       map[string]any `json:"extra_config"`
	IsDefault         bool           `json:"is_default"`
	Status            string         `json:"status"`
	Environment       string         `json:"environment"`
	BusinessTags      []string       `json:"business_tags"`
	ReferenceCount    int64          `json:"reference_count"`
	TotalTransactions int64          `json:"total_transactions"`
	TotalAmountCent   int64          `json:"total_amount_cent"`
	LastUsedAt        *string        `json:"last_used_at"`
	CreatedBy         *uint64        `json:"created_by"`
	UpdatedBy         *uint64        `json:"updated_by"`
	CreatedAt         string         `json:"created_at"`
	UpdatedAt         string         `json:"updated_at"`
}

// CreateRequest 创建支付配置请求
type CreateRequest struct {
	Name         string         `json:"name" binding:"required"`
	Provider     string         `json:"provider" binding:"required,oneof=leshua lakala shuncheng mock"`
	MerchantID   string         `json:"merchant_id" binding:"required"`
	GatewayURL   string         `json:"gateway_url"`
	SignKey      string         `json:"sign_key"`
	NotifyKey    string         `json:"notify_key"`
	NotifyURL    string         `json:"notify_url"`
	JumpURL      string         `json:"jump_url"`
	PayWay       string         `json:"pay_way"`
	JSPayFlag    string         `json:"jspay_flag"`
	SignType     string         `json:"sign_type"`
	ExtraConfig  map[string]any `json:"extra_config"`
	IsDefault    bool           `json:"is_default"`
	Status       string         `json:"status" binding:"omitempty,oneof=active disabled testing"`
	Environment  string         `json:"environment" binding:"omitempty,oneof=production sandbox"`
	BusinessTags []string       `json:"business_tags"`
}

// UpdateRequest 更新支付配置请求
type UpdateRequest struct {
	Name         *string        `json:"name"`
	MerchantID   *string        `json:"merchant_id"`
	GatewayURL   *string        `json:"gateway_url"`
	SignKey      *string        `json:"sign_key"`
	NotifyKey    *string        `json:"notify_key"`
	NotifyURL    *string        `json:"notify_url"`
	JumpURL      *string        `json:"jump_url"`
	PayWay       *string        `json:"pay_way"`
	JSPayFlag    *string        `json:"jspay_flag"`
	SignType     *string        `json:"sign_type"`
	ExtraConfig  map[string]any `json:"extra_config"`
	IsDefault    *bool          `json:"is_default"`
	Status       *string        `json:"status"`
	Environment  *string        `json:"environment"`
	BusinessTags []string       `json:"business_tags"`
}

// ListQuery 列表查询参数
type ListQuery struct {
	Provider    string `form:"provider"`
	PayWay      string `form:"pay_way"`
	Status      string `form:"status"`
	Environment string `form:"environment"`
	Page        int    `form:"page"`
	PageSize    int    `form:"page_size"`
}

// ListResponse 列表响应
type ListResponse struct {
	Items    []ConfigDTO `json:"items"`
	Total    int64       `json:"total"`
	Page     int         `json:"page"`
	PageSize int         `json:"page_size"`
}

// ExportBackup 支付配置备份导出内容。
type ExportBackup struct {
	Type       string      `json:"type"`
	Version    int         `json:"version"`
	ExportedAt string      `json:"exported_at"`
	ExportedBy uint64      `json:"exported_by"`
	Total      int         `json:"total"`
	Configs    []ConfigDTO `json:"configs"`
}

// ImportBackupResult 支付配置备份导入结果。
type ImportBackupResult struct {
	Total   int `json:"total"`
	Created int `json:"created"`
	Updated int `json:"updated"`
}
