package knowledgeqa

import (
	"strings"
	"time"

	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

// RootHandleRecord is the durable server-only authorization snapshot for one
// Q&A turn root. RepositoryRef is never serialized through the runtime API.
type RootHandleRecord struct {
	ID              string    `gorm:"primaryKey;size:64"`
	OrganizationID  string    `gorm:"size:64;not null;index:idx_kqa_root_authority,priority:1"`
	WorkspaceID     string    `gorm:"size:64;not null;index:idx_kqa_root_authority,priority:2"`
	RequesterID     string    `gorm:"size:64;not null;index:idx_kqa_root_authority,priority:3"`
	TurnID          string    `gorm:"size:64;not null;index:idx_kqa_root_authority,priority:4;index:idx_kqa_root_turn"`
	KnowledgeBaseID string    `gorm:"size:64;not null;index"`
	SourceID        string    `gorm:"size:64;not null;default:'';index"`
	SourceName      string    `gorm:"size:255;not null;default:''"`
	RootKind        string    `gorm:"size:32;not null"`
	RepoRole        string    `gorm:"size:64;not null"`
	Revision        string    `gorm:"size:128;not null"`
	RepositoryRef   string    `gorm:"size:255;not null"`
	DefaultRef      string    `gorm:"size:255;not null;default:''"`
	ResolvedRef     string    `gorm:"size:255;not null;default:''"`
	ResolvedRefKind string    `gorm:"size:16;not null;default:''"`
	FreshnessStatus string    `gorm:"size:64;not null;default:''"`
	ExpiresAt       time.Time `gorm:"not null;index"`
	CreatedAt       time.Time `gorm:"not null"`
}

func (RootHandleRecord) TableName() string {
	return "knowledge_qa_root_handles"
}

// AccountingCheckpointRecord contains only aggregate usage and settlement
// lineage for one requester-bound runtime attempt. It never stores prompts,
// repository content, answers, evidence, or credentials.
type AccountingCheckpointRecord struct {
	AttemptID          string                `gorm:"primaryKey;size:64"`
	OrganizationID     string                `gorm:"size:64;not null"`
	WorkspaceID        string                `gorm:"size:64;not null"`
	RequesterID        string                `gorm:"size:64;not null"`
	TurnID             string                `gorm:"size:64;not null"`
	Generation         int                   `gorm:"not null"`
	State              string                `gorm:"size:16;not null"`
	UsageJSON          string                `gorm:"type:text;not null"`
	SettlementRefsJSON settlementLineageJSON `gorm:"not null"`
	ExpiresAt          time.Time             `gorm:"not null;index:idx_kqa_accounting_expiry"`
	CreatedAt          time.Time             `gorm:"not null"`
	UpdatedAt          time.Time             `gorm:"not null"`
}

func (AccountingCheckpointRecord) TableName() string {
	return "knowledge_qa_accounting_checkpoints"
}

// settlementLineageJSON contains bounded provider settlement references and
// synthetic kqa_request_sha256_* recovery-correlation references. The runtime
// and store enforce a 4-MiB encoded-lineage and 4,096-reference cap. Together
// with the 65,535-byte usage cap, this leaves substantial row-codec and
// fixed-column headroom below TiDB's default 6-MiB transaction-entry limit;
// LONGTEXT's nominal 4-GiB column length is not treated as the effective limit.
type settlementLineageJSON string

func (settlementLineageJSON) GormDataType() string {
	return "text"
}

func (settlementLineageJSON) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
	if db != nil && strings.EqualFold(db.Dialector.Name(), "mysql") {
		return "LONGTEXT"
	}
	return "TEXT"
}
