package model

import (
	"time"

	"gorm.io/gorm"
)

// Admin is not a database model. It's configured via environment variables.

// Master represents a tenant account.
type Master struct {
	gorm.Model
	Name             string `gorm:"size:255" json:"name"`
	MasterKey        string `gorm:"size:255" json:"-"`                       // bcrypt hash of master key
	MasterKeyDigest  string `gorm:"size:64;uniqueIndex" json:"-"`            // sha256 digest for lookup
	Group            string `gorm:"size:100;default:'default'" json:"group"` // routing group
	DefaultNamespace string `gorm:"size:100;default:'default'" json:"default_namespace"`
	Namespaces       string `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces
	Epoch            int64  `gorm:"default:1" json:"epoch"`                        // used for revocation/rotation
	Status           string `gorm:"size:50;default:'active'" json:"status"`        // active, suspended
	MaxChildKeys     int    `gorm:"default:5" json:"max_child_keys"`
	GlobalQPS        int    `gorm:"default:0" json:"global_qps"`
}

// Key represents a child access token issued by a Master.
type Key struct {
	gorm.Model
	MasterID           uint       `gorm:"not null;index" json:"master_id"`
	KeySecret          string     `gorm:"size:255;column:key_secret" json:"-"`     // bcrypt hash of child key
	TokenHash          string     `gorm:"size:64;uniqueIndex" json:"token_hash"`   // sha256 digest of child key
	Group              string     `gorm:"size:100;default:'default'" json:"group"` // routing group
	Scopes             string     `gorm:"size:1024" json:"scopes"`                 // Comma-separated scopes
	DefaultNamespace   string     `gorm:"size:100;default:'default'" json:"default_namespace"`
	Namespaces         string     `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces
	IssuedAtEpoch      int64      `gorm:"not null" json:"issued_at_epoch"`               // copy of master epoch at issuance
	Status             string     `gorm:"size:50;default:'active'" json:"status"`        // active, suspended
	IssuedBy           string     `gorm:"size:20;default:'master'" json:"issued_by"`
	ModelLimits        string     `gorm:"size:2048" json:"model_limits"`
	ModelLimitsEnabled bool       `gorm:"default:false" json:"model_limits_enabled"`
	ExpiresAt          *time.Time `gorm:"index" json:"expires_at"`
	AllowIPs           string     `gorm:"size:1024" json:"allow_ips"`
	DenyIPs            string     `gorm:"size:1024" json:"deny_ips"`
	LastAccessedAt     *time.Time `json:"last_accessed_at"`
	RequestCount       int64      `gorm:"default:0" json:"request_count"`
	UsedTokens         int64      `gorm:"default:0" json:"used_tokens"`
	QuotaLimit         int64      `gorm:"default:-1" json:"quota_limit"`
	QuotaUsed          int64      `gorm:"default:0" json:"quota_used"`
	QuotaResetAt       *time.Time `json:"quota_reset_at"`
	QuotaResetType     string     `gorm:"size:20" json:"quota_reset_type"`
}

// Model remains the same.
type Model struct {
	gorm.Model
	Name               string  `gorm:"uniqueIndex;not null" json:"name"`
	Kind               string  `gorm:"size:50;default:'chat'" json:"kind"`
	ContextWindow      int     `json:"context_window"`
	CostPerToken       float64 `json:"cost_per_token"`
	SupportsVision     bool    `json:"supports_vision"`
	SupportsFunctions  bool    `json:"supports_functions"`
	SupportsToolChoice bool    `json:"supports_tool_choice"`
	SupportsFIM        bool    `json:"supports_fim"`
	MaxOutputTokens    int     `json:"max_output_tokens"`
}

// Binding defines a stable "namespace.public_model" routing key and its target ProviderGroup + selector.
type Binding struct {
	gorm.Model
	Namespace     string `gorm:"size:100;not null;index:idx_binding_key,unique" json:"namespace"`
	PublicModel   string `gorm:"size:255;not null;index:idx_binding_key,unique" json:"public_model"`
	GroupID       uint   `gorm:"not null;index:idx_binding_key,unique" json:"group_id"`
	Weight        int    `gorm:"default:1" json:"weight"`
	SelectorType  string `gorm:"size:50;default:'exact'" json:"selector_type"`
	SelectorValue string `gorm:"size:255" json:"selector_value"`
	Status        string `gorm:"size:50;default:'active'" json:"status"`
}
