package model

import "time"

type APIKeyStatus string

const (
	APIKeyStatusActive  APIKeyStatus = "active"
	APIKeyStatusRevoked APIKeyStatus = "revoked"
	APIKeyStatusExpired APIKeyStatus = "expired"
)

type APIKey struct {
	Base
	KeyPrefix       string       `gorm:"size:32;index" json:"key_prefix"` // 明文前若干位(如16),展示/识别
	KeyHash         string       `gorm:"size:64;uniqueIndex" json:"-"`    // sha256 hex, 不输出
	AgentName       string       `gorm:"size:100;index" json:"agent_name"`
	OwnerUserID     uint64       `gorm:"index" json:"owner_user_id"`
	Owner           *User        `gorm:"foreignKey:OwnerUserID" json:"owner,omitempty"`
	PermissionScope StringArray  `gorm:"type:json" json:"permission_scope"` // ["caasm:view_assets",...]
	Status          APIKeyStatus `gorm:"size:20;index;default:'active'" json:"status"`
	ExpiresAt       *time.Time   `json:"expires_at,omitempty"` // nil=永久
	LastUsedAt      *time.Time   `json:"last_used_at,omitempty"`
}
