package models

import (
	"time"
)


// ALPSnapshot records periodic ALP pool stats for APR calculation
type ALPSnapshot struct {
	ID           uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	PoolValue    float64   `gorm:"type:decimal(30,18)" json:"pool_value"`    // getAumInUsdy(true), 18 dec
	UsdySupply   float64   `gorm:"type:decimal(30,18)" json:"usdy_supply"`   // USDY.totalSupply(), 18 dec
	FeeSurplus   float64   `gorm:"type:decimal(30,18)" json:"fee_surplus"`   // poolValue - usdySupply
	ALPPrice     float64   `gorm:"type:decimal(30,18)" json:"alp_price"`     // getPrice(false), 30 dec
	SnapshotTime time.Time `gorm:"not null;index" json:"snapshot_time"`
	CreatedAt    time.Time `json:"created_at"`
}

func (ALPSnapshot) TableName() string {
	return "alp_snapshots"
}

// APYSnapshot represents the apy_snapshots table
type APYSnapshot struct {
	ID              uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	AssetID         uint      `gorm:"not null;index:idx_asset_time" json:"asset_id"`
	ChainID         int       `gorm:"not null" json:"chain_id"`
	ContractAddress string    `gorm:"size:42" json:"contract_address"`
	APYValue        float64   `gorm:"type:decimal(10,4)" json:"apy_value"`
	SupplyAPY    float64   `gorm:"type:decimal(20,4)" json:"supply_apy"`
	BorrowAPY    float64   `gorm:"type:decimal(20,4)" json:"borrow_apy"`
	TotalAssets  float64   `gorm:"type:decimal(30,18)" json:"total_assets"`
	TotalSupply  float64   `gorm:"type:decimal(30,18)" json:"total_supply"`
	Price        float64   `gorm:"type:decimal(30,18)" json:"price"`
	SnapshotTime time.Time `gorm:"not null;index:idx_asset_time" json:"snapshot_time"`
	CreatedAt    time.Time `json:"created_at"`
}

func (APYSnapshot) TableName() string {
	return "apy_snapshots"
}

// ProductLink represents the product_links table — per-asset links shown on the product detail page
type ProductLink struct {
	ID           uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	AssetID      uint      `gorm:"not null;index" json:"asset_id"`
	LinkText     string    `gorm:"size:100;not null" json:"link_text"`
	LinkURL      string    `gorm:"type:text;not null" json:"link_url"`
	Description  string    `gorm:"size:500" json:"description"`
	DisplayArea  string    `gorm:"size:20;default:protocol" json:"display_area"`
	DisplayOrder int       `gorm:"default:0" json:"display_order"`
	IsActive     bool      `gorm:"default:true" json:"is_active"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

func (ProductLink) TableName() string {
	return "product_links"
}

// KLineData represents the kline_data table
type KLineData struct {
	ID              uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	AssetID         uint      `gorm:"not null;index:idx_asset_time" json:"asset_id"`
	ChainID         int       `gorm:"not null" json:"chain_id"`
	ContractAddress string    `gorm:"size:42" json:"contract_address"`
	Timeframe       string    `gorm:"size:10;not null" json:"timeframe"`
	OpenTime     time.Time `gorm:"not null;index:idx_asset_time" json:"open_time"`
	CloseTime    time.Time `gorm:"not null" json:"close_time"`
	OpenPrice    float64   `gorm:"type:decimal(30,18);not null" json:"open_price"`
	HighPrice    float64   `gorm:"type:decimal(30,18);not null" json:"high_price"`
	LowPrice     float64   `gorm:"type:decimal(30,18);not null" json:"low_price"`
	ClosePrice   float64   `gorm:"type:decimal(30,18);not null" json:"close_price"`
	Volume       float64   `gorm:"type:decimal(30,18)" json:"volume"`
	Trades       int       `json:"trades"`
	CreatedAt    time.Time `json:"created_at"`
}

func (KLineData) TableName() string {
	return "kline_data"
}


// PointsRule represents the points_rules table
type PointsRule struct {
	ID          uint       `gorm:"primaryKey;autoIncrement" json:"id"`
	RuleName    string     `gorm:"size:100;not null;unique" json:"rule_name"`
	RuleType    string     `gorm:"size:50;not null" json:"rule_type"`
	BasePoints  int        `gorm:"not null" json:"base_points"`
	Multiplier  float64    `gorm:"type:decimal(10,4);default:1.0000" json:"multiplier"`
	Conditions  string     `gorm:"type:json" json:"conditions"`
	Description string     `gorm:"type:text" json:"description"`
	ValidFrom   NullTime   `json:"valid_from"`
	ValidUntil  NullTime   `json:"valid_until"`
	IsActive    bool       `gorm:"default:true" json:"is_active"`
	Priority    int        `gorm:"default:0" json:"priority"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`
}

func (PointsRule) TableName() string {
	return "points_rules"
}

// Role represents the roles table
type Role struct {
	ID          uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	RoleName    string    `gorm:"size:50;not null;unique" json:"role_name"`
	Description string    `gorm:"type:text" json:"description"`
	Permissions string    `gorm:"type:json" json:"permissions"`
	IsActive    bool      `gorm:"default:true" json:"is_active"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

func (Role) TableName() string {
	return "roles"
}

// UserRole represents the user_roles table
type UserRole struct {
	ID         uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID     uint      `gorm:"not null;uniqueIndex:uk_user_role" json:"user_id"`
	RoleID     uint      `gorm:"not null;uniqueIndex:uk_user_role" json:"role_id"`
	AssignedAt time.Time `gorm:"not null" json:"assigned_at"`
	AssignedBy uint      `json:"assigned_by"`
	CreatedAt  time.Time `json:"created_at"`
}

func (UserRole) TableName() string {
	return "user_roles"
}

// Session represents the sessions table
type Session struct {
	ID             uint       `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID         uint       `gorm:"not null;index" json:"user_id"`
	SessionToken   string     `gorm:"size:255;not null;unique" json:"session_token"`
	WalletAddress  string     `gorm:"size:42;not null" json:"wallet_address"`
	SignMessage    string     `gorm:"type:text" json:"sign_message"`
	Signature      string     `gorm:"size:132" json:"signature"`
	SignatureHash  string     `gorm:"size:66" json:"signature_hash"`
	IPAddress      string     `gorm:"size:45" json:"ip_address"`
	UserAgent      string     `gorm:"type:text" json:"user_agent"`
	ExpiresAt      *time.Time `gorm:"index" json:"expires_at"`
	LastActivityAt *time.Time `json:"last_activity_at"`
	CreatedAt      time.Time  `json:"created_at"`
}

func (Session) TableName() string {
	return "sessions"
}

// Transaction represents the transactions table
type Transaction struct {
	ID              uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID          uint      `gorm:"not null;index:idx_user_time" json:"user_id"`
	TxHash          string    `gorm:"size:66;not null;unique" json:"tx_hash"`
	ChainID         int       `gorm:"not null" json:"chain_id"`
	BlockNumber     int64     `gorm:"not null" json:"block_number"`
	TxType          string    `gorm:"size:50;not null" json:"tx_type"`
	AssetID         *uint     `json:"asset_id"`
	FromAddress     string    `gorm:"size:42;not null" json:"from_address"`
	ToAddress       string    `gorm:"size:42;not null" json:"to_address"`
	Amount          float64   `gorm:"type:decimal(30,18)" json:"amount"`
	TokenSymbol     string    `gorm:"size:20" json:"token_symbol"`
	GasUsed         int64     `json:"gas_used"`
	GasPrice        float64   `gorm:"type:decimal(30,18)" json:"gas_price"`
	Status          string    `gorm:"size:20;not null" json:"status"`
	ErrorMessage    string    `gorm:"type:text" json:"error_message"`
	Metadata        string    `gorm:"type:json" json:"metadata"`
	ConfirmedAt     time.Time `gorm:"not null;index:idx_user_time" json:"confirmed_at"`
	CreatedAt       time.Time `json:"created_at"`
}

func (Transaction) TableName() string {
	return "transactions"
}

// OperationLog represents the operation_logs table
type OperationLog struct {
	ID            uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID        *uint     `gorm:"index" json:"user_id"`
	OperationType string    `gorm:"size:50;not null" json:"operation_type"`
	TargetType    string    `gorm:"size:50" json:"target_type"`
	TargetID      *uint     `json:"target_id"`
	Action        string    `gorm:"size:100;not null" json:"action"`
	Changes       string    `gorm:"type:json" json:"changes"`
	IPAddress     string    `gorm:"size:45" json:"ip_address"`
	UserAgent     string    `gorm:"type:text" json:"user_agent"`
	Status        string    `gorm:"size:20;not null" json:"status"`
	ErrorMessage  string    `gorm:"type:text" json:"error_message"`
	CreatedAt     time.Time `gorm:"index" json:"created_at"`
}

func (OperationLog) TableName() string {
	return "operation_logs"
}

// UserActivity represents the user_activities table
type UserActivity struct {
	ID             uint      `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID         uint      `gorm:"not null;index:idx_user_time" json:"user_id"`
	ActivityType   string    `gorm:"size:50;not null" json:"activity_type"`
	ActivityData   string    `gorm:"type:json" json:"activity_data"`
	AssetID        *uint     `json:"asset_id"`
	Amount         float64   `gorm:"type:decimal(30,18)" json:"amount"`
	PointsEarned   int       `gorm:"default:0" json:"points_earned"`
	ReferenceType  string    `gorm:"size:50" json:"reference_type"`
	ReferenceID    *uint     `json:"reference_id"`
	IPAddress      string    `gorm:"size:45" json:"ip_address"`
	CreatedAt      time.Time `gorm:"index:idx_user_time" json:"created_at"`
}

func (UserActivity) TableName() string {
	return "user_activities"
}
