package identity

import (
	"time"
)

type User struct {
	ID                     int64      `gorm:"column:id;primaryKey" json:"id"`
	Email                  *string    `gorm:"column:email" json:"email"`
	Username               string     `gorm:"column:username" json:"username"`
	HashedPassword         *string    `gorm:"column:hashed_password" json:"-"`
	Phone                  *string    `gorm:"column:phone" json:"phone"`
	IDCardNo               *string    `gorm:"column:id_card_no" json:"-"`
	IDCardNoHash           *string    `gorm:"column:id_card_no_hash" json:"-"`
	RealName               *string    `gorm:"column:real_name" json:"-"`
	IDCardVerified         string     `gorm:"column:id_card_verified" json:"id_card_verified"`
	IDCardVerifiedAt       *time.Time `gorm:"column:id_card_verified_at" json:"id_card_verified_at,omitempty"`
	IDCardFrontImage       *string    `gorm:"column:id_card_front_image" json:"-"`
	IDCardBackImage        *string    `gorm:"column:id_card_back_image" json:"-"`
	IDCardFaceImage        *string    `gorm:"column:id_card_face_image" json:"-"`
	IDCardVerifyRemark     *string    `gorm:"column:id_card_verify_remark" json:"-"`
	Role                   string     `gorm:"column:role" json:"role"`
	Status                 string     `gorm:"column:status" json:"status"`
	IsEmailVerified        bool       `gorm:"column:is_email_verified" json:"is_email_verified"`
	IsPhoneVerified        bool       `gorm:"column:is_phone_verified" json:"is_phone_verified"`
	Avatar                 *string    `gorm:"column:avatar" json:"avatar"`
	Nickname               *string    `gorm:"column:nickname" json:"nickname"`
	Balance                int64      `gorm:"column:balance" json:"balance"`
	LastLoginAt            *time.Time `gorm:"column:last_login_at" json:"last_login_at,omitempty"`
	FullRefundQuotaExtra   int        `gorm:"column:full_refund_quota_extra" json:"-"`
	FullRefundQuotaResetAt *time.Time `gorm:"column:full_refund_quota_reset_at" json:"-"`
	InviteCodeChangeCount  int        `gorm:"column:invite_code_change_count" json:"-"`
	InviteCodeChangeExtra  int        `gorm:"column:invite_code_change_extra" json:"-"`
	InviteCode             *string    `gorm:"column:invite_code" json:"-"`
	BoundAgentID           *int64     `gorm:"column:bound_agent_id" json:"-"`
	CreatedAt              time.Time  `gorm:"column:created_at" json:"created_at"`
	UpdatedAt              time.Time  `gorm:"column:updated_at" json:"updated_at"`
}

func (User) TableName() string {
	return "users"
}

type UserResponse struct {
	ID              int64     `json:"id"`
	Email           *string   `json:"email"`
	Username        string    `json:"username"`
	Phone           *string   `json:"phone"`
	Avatar          *string   `json:"avatar"`
	Nickname        *string   `json:"nickname"`
	RealName        *string   `json:"real_name"`
	IDCardVerified  string    `json:"id_card_verified"`
	Role            string    `json:"role"`
	Status          string    `json:"status"`
	IsEmailVerified bool      `json:"is_email_verified"`
	HasPassword     bool      `json:"has_password"`
	Balance         int64     `json:"balance"`
	CreatedAt       time.Time `json:"created_at"`
}

type AdminImpersonationAudit struct {
	ID             int64     `gorm:"column:id;primaryKey"`
	OperatorID     int64     `gorm:"column:operator_id"`
	TargetUserID   int64     `gorm:"column:target_user_id"`
	Reason         string    `gorm:"column:reason"`
	IPAddress      *string   `gorm:"column:ip_address"`
	UserAgent      *string   `gorm:"column:user_agent"`
	TokenJTI       string    `gorm:"column:token_jti"`
	TokenExpiresAt time.Time `gorm:"column:token_expires_at"`
	CreatedAt      time.Time `gorm:"column:created_at"`
}

func (AdminImpersonationAudit) TableName() string {
	return "admin_impersonation_audits"
}

func NewUserResponse(user User, secret string) UserResponse {
	return UserResponse{
		ID:              user.ID,
		Email:           user.Email,
		Username:        user.Username,
		Phone:           user.Phone,
		Avatar:          user.Avatar,
		Nickname:        user.Nickname,
		RealName:        MaskName(DecryptOptional(user.RealName, secret)),
		IDCardVerified:  user.IDCardVerified,
		Role:            user.Role,
		Status:          user.Status,
		IsEmailVerified: user.IsEmailVerified,
		HasPassword:     user.HashedPassword != nil,
		Balance:         user.Balance,
		CreatedAt:       user.CreatedAt,
	}
}

type SystemSetting struct {
	ID          int64        `gorm:"column:id;primaryKey"`
	Key         string       `gorm:"column:key"`
	Value       SettingValue `gorm:"column:value"`
	Label       string       `gorm:"column:label"`
	Description *string      `gorm:"column:description"`
	CreatedAt   time.Time    `gorm:"column:created_at"`
	UpdatedAt   time.Time    `gorm:"column:updated_at"`
}

func (SystemSetting) TableName() string {
	return "system_settings"
}

type UserInviteCode struct {
	ID        int64     `gorm:"column:id;primaryKey"`
	UserID    int64     `gorm:"column:user_id"`
	Code      string    `gorm:"column:code"`
	IsCurrent bool      `gorm:"column:is_current"`
	Source    string    `gorm:"column:source"`
	CreatedAt time.Time `gorm:"column:created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at"`
}

type UserVerificationRecord struct {
	ID                int64        `gorm:"column:id;primaryKey"`
	UserID            int64        `gorm:"column:user_id"`
	RealName          *string      `gorm:"column:real_name"`
	IDCardNo          *string      `gorm:"column:id_card_no"`
	IDCardNoHash      *string      `gorm:"column:id_card_no_hash"`
	IDCardFrontImage  *string      `gorm:"column:id_card_front_image"`
	IDCardBackImage   *string      `gorm:"column:id_card_back_image"`
	IDCardFaceImage   *string      `gorm:"column:id_card_face_image"`
	Provider          string       `gorm:"column:provider"`
	Mode              string       `gorm:"column:mode"`
	ProviderTaskID    *string      `gorm:"column:provider_task_id"`
	Status            string       `gorm:"column:status"`
	ProviderResponse  SettingValue `gorm:"column:provider_response"`
	MaterialExpiresAt *time.Time   `gorm:"column:material_expires_at"`
	MaterialCleaned   bool         `gorm:"column:material_cleaned"`
	VerifiedAt        *time.Time   `gorm:"column:verified_at"`
	RejectedAt        *time.Time   `gorm:"column:rejected_at"`
	RejectReason      *string      `gorm:"column:reject_reason"`
	CreatedAt         time.Time    `gorm:"column:created_at"`
	UpdatedAt         time.Time    `gorm:"column:updated_at"`
}

func (UserVerificationRecord) TableName() string {
	return "user_verification_records"
}

type UserVerificationQuota struct {
	ID            int64     `gorm:"column:id;primaryKey"`
	UserID        int64     `gorm:"column:user_id"`
	FreeUsed      int       `gorm:"column:free_used"`
	PaidRemaining int       `gorm:"column:paid_remaining"`
	PaidPurchased int       `gorm:"column:paid_purchased"`
	PaidUsed      int       `gorm:"column:paid_used"`
	CreatedAt     time.Time `gorm:"column:created_at"`
	UpdatedAt     time.Time `gorm:"column:updated_at"`
}

func (UserVerificationQuota) TableName() string {
	return "user_verification_quotas"
}

func (UserInviteCode) TableName() string {
	return "user_invite_codes"
}

type UserAgent struct {
	ID         int64      `gorm:"column:id;primaryKey"`
	UserID     int64      `gorm:"column:user_id"`
	Status     string     `gorm:"column:status"`
	InviteCode *string    `gorm:"column:invite_code"`
	ApprovedAt *time.Time `gorm:"column:approved_at"`
	ApprovedBy *int64     `gorm:"column:approved_by"`
	CreatedAt  time.Time  `gorm:"column:created_at"`
	UpdatedAt  time.Time  `gorm:"column:updated_at"`
}

func (UserAgent) TableName() string {
	return "user_agents"
}

type AgentCustomer struct {
	ID             int64     `gorm:"column:id;primaryKey"`
	AgentUserID    int64     `gorm:"column:agent_user_id"`
	CustomerUserID int64     `gorm:"column:customer_user_id"`
	BindSource     string    `gorm:"column:bind_source"`
	BoundAt        time.Time `gorm:"column:bound_at"`
	FirstOrderID   *int64    `gorm:"column:first_order_id"`
	Status         string    `gorm:"column:status"`
	CreatedAt      time.Time `gorm:"column:created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at"`
}

func (AgentCustomer) TableName() string {
	return "agent_customers"
}

type UserOAuthConnection struct {
	ID          int64        `gorm:"column:id;primaryKey"`
	UserID      int64        `gorm:"column:user_id"`
	PluginID    string       `gorm:"column:plugin_id"`
	ProviderUID string       `gorm:"column:provider_uid"`
	AccessToken *string      `gorm:"column:access_token"`
	Metadata    SettingValue `gorm:"column:metadata"`
	CreatedAt   time.Time    `gorm:"column:created_at"`
	UpdatedAt   time.Time    `gorm:"column:updated_at"`
}

func (UserOAuthConnection) TableName() string {
	return "user_oauth_connections"
}

type AffiliateClick struct {
	ID               int64      `gorm:"column:id;primaryKey"`
	AffCode          string     `gorm:"column:aff_code"`
	AgentID          *int64     `gorm:"column:agent_id"`
	RegisteredUserID *int64     `gorm:"column:registered_user_id"`
	ConvertedAt      *time.Time `gorm:"column:converted_at"`
	CreatedAt        time.Time  `gorm:"column:created_at"`
	UpdatedAt        time.Time  `gorm:"column:updated_at"`
}

func (AffiliateClick) TableName() string {
	return "affiliate_clicks"
}
