package storage

import (
	"time"

	"chorus.services/bzzz/pkg/ucxl"
	"chorus.services/bzzz/pkg/crypto"
	slurpContext "chorus.services/bzzz/pkg/slurp/context"
)

// DatabaseSchema defines the complete schema for encrypted context storage
// This includes both relational and document-based structures for hybrid storage

// ContextRecord represents the main context storage record
type ContextRecord struct {
	// Primary identification
	ID              string           `json:"id" db:"id"`                     // Unique record ID
	UCXLAddress     ucxl.Address     `json:"ucxl_address" db:"ucxl_address"` // UCXL address
	Path            string           `json:"path" db:"path"`                 // File system path
	PathHash        string           `json:"path_hash" db:"path_hash"`       // Hash of path for indexing
	
	// Core context data
	Summary         string           `json:"summary" db:"summary"`
	Purpose         string           `json:"purpose" db:"purpose"`
	Technologies    []byte           `json:"technologies" db:"technologies"` // JSON array
	Tags            []byte           `json:"tags" db:"tags"`                 // JSON array
	Insights        []byte           `json:"insights" db:"insights"`         // JSON array
	
	// Hierarchy control
	OverridesParent     bool         `json:"overrides_parent" db:"overrides_parent"`
	ContextSpecificity  int          `json:"context_specificity" db:"context_specificity"`
	AppliesToChildren   bool         `json:"applies_to_children" db:"applies_to_children"`
	
	// Quality metrics
	RAGConfidence       float64      `json:"rag_confidence" db:"rag_confidence"`
	StalenessScore      float64      `json:"staleness_score" db:"staleness_score"`
	ValidationScore     float64      `json:"validation_score" db:"validation_score"`
	
	// Versioning
	Version             int64        `json:"version" db:"version"`
	ParentVersion       *int64       `json:"parent_version" db:"parent_version"`
	ContextHash         string       `json:"context_hash" db:"context_hash"`
	
	// Temporal metadata
	CreatedAt           time.Time    `json:"created_at" db:"created_at"`
	UpdatedAt           time.Time    `json:"updated_at" db:"updated_at"`
	GeneratedAt         time.Time    `json:"generated_at" db:"generated_at"`
	LastAccessedAt      *time.Time   `json:"last_accessed_at" db:"last_accessed_at"`
	ExpiresAt           *time.Time   `json:"expires_at" db:"expires_at"`
	
	// Storage metadata
	StorageType         string       `json:"storage_type" db:"storage_type"`     // local, distributed, hybrid
	CompressionType     string       `json:"compression_type" db:"compression_type"`
	EncryptionLevel     int          `json:"encryption_level" db:"encryption_level"`
	ReplicationFactor   int          `json:"replication_factor" db:"replication_factor"`
	Checksum            string       `json:"checksum" db:"checksum"`
	DataSize            int64        `json:"data_size" db:"data_size"`
	CompressedSize      int64        `json:"compressed_size" db:"compressed_size"`
}

// EncryptedContextRecord represents role-based encrypted context storage
type EncryptedContextRecord struct {
	// Primary keys
	ID              string                      `json:"id" db:"id"`
	ContextID       string                      `json:"context_id" db:"context_id"`       // FK to ContextRecord
	Role            string                      `json:"role" db:"role"`
	UCXLAddress     ucxl.Address                `json:"ucxl_address" db:"ucxl_address"`
	
	// Encryption details
	AccessLevel     slurpContext.RoleAccessLevel `json:"access_level" db:"access_level"`
	EncryptedData   []byte                      `json:"encrypted_data" db:"encrypted_data"`
	KeyFingerprint  string                      `json:"key_fingerprint" db:"key_fingerprint"`
	EncryptionAlgo  string                      `json:"encryption_algo" db:"encryption_algo"`
	KeyVersion      int                         `json:"key_version" db:"key_version"`
	
	// Data integrity
	DataChecksum    string                      `json:"data_checksum" db:"data_checksum"`
	EncryptionHash  string                      `json:"encryption_hash" db:"encryption_hash"`
	
	// Temporal data
	CreatedAt       time.Time                   `json:"created_at" db:"created_at"`
	UpdatedAt       time.Time                   `json:"updated_at" db:"updated_at"`
	LastDecryptedAt *time.Time                  `json:"last_decrypted_at" db:"last_decrypted_at"`
	ExpiresAt       *time.Time                  `json:"expires_at" db:"expires_at"`
	
	// Access tracking
	AccessCount     int64                       `json:"access_count" db:"access_count"`
	LastAccessedBy  string                      `json:"last_accessed_by" db:"last_accessed_by"`
	AccessHistory   []byte                      `json:"access_history" db:"access_history"` // JSON access log
}

// ContextHierarchyRecord represents hierarchical relationships between contexts
type ContextHierarchyRecord struct {
	ID              string       `json:"id" db:"id"`
	ParentAddress   ucxl.Address `json:"parent_address" db:"parent_address"`
	ChildAddress    ucxl.Address `json:"child_address" db:"child_address"`
	ParentPath      string       `json:"parent_path" db:"parent_path"`
	ChildPath       string       `json:"child_path" db:"child_path"`
	
	// Relationship metadata
	RelationshipType string      `json:"relationship_type" db:"relationship_type"` // parent, sibling, dependency
	InheritanceWeight float64    `json:"inheritance_weight" db:"inheritance_weight"`
	OverrideStrength int         `json:"override_strength" db:"override_strength"`
	Distance         int         `json:"distance" db:"distance"` // Hierarchy depth distance
	
	// Temporal tracking
	CreatedAt       time.Time    `json:"created_at" db:"created_at"`
	ValidatedAt     time.Time    `json:"validated_at" db:"validated_at"`
	LastResolvedAt  *time.Time   `json:"last_resolved_at" db:"last_resolved_at"`
	
	// Resolution statistics
	ResolutionCount int64        `json:"resolution_count" db:"resolution_count"`
	ResolutionTime  float64      `json:"resolution_time" db:"resolution_time"` // Average ms
}

// DecisionHopRecord represents temporal decision analysis storage
type DecisionHopRecord struct {
	// Primary identification
	ID              string                         `json:"id" db:"id"`
	DecisionID      string                         `json:"decision_id" db:"decision_id"`
	UCXLAddress     ucxl.Address                   `json:"ucxl_address" db:"ucxl_address"`
	ContextVersion  int64                          `json:"context_version" db:"context_version"`
	
	// Decision metadata
	ChangeReason    string                         `json:"change_reason" db:"change_reason"`
	DecisionMaker   string                         `json:"decision_maker" db:"decision_maker"`
	DecisionRationale string                       `json:"decision_rationale" db:"decision_rationale"`
	ImpactScope     string                         `json:"impact_scope" db:"impact_scope"`
	ConfidenceLevel float64                        `json:"confidence_level" db:"confidence_level"`
	
	// Context evolution
	PreviousHash    string                         `json:"previous_hash" db:"previous_hash"`
	CurrentHash     string                         `json:"current_hash" db:"current_hash"`
	ContextDelta    []byte                         `json:"context_delta" db:"context_delta"` // JSON diff
	StalenessScore  float64                        `json:"staleness_score" db:"staleness_score"`
	
	// Temporal data
	Timestamp       time.Time                      `json:"timestamp" db:"timestamp"`
	PreviousDecisionTime *time.Time                `json:"previous_decision_time" db:"previous_decision_time"`
	ProcessingTime  float64                        `json:"processing_time" db:"processing_time"` // ms
	
	// External references
	ExternalRefs    []byte                         `json:"external_refs" db:"external_refs"` // JSON array
	CommitHash      string                         `json:"commit_hash" db:"commit_hash"`
	TicketID        string                         `json:"ticket_id" db:"ticket_id"`
}

// DecisionInfluenceRecord represents decision influence relationships
type DecisionInfluenceRecord struct {
	ID                  string       `json:"id" db:"id"`
	SourceDecisionID    string       `json:"source_decision_id" db:"source_decision_id"`
	TargetDecisionID    string       `json:"target_decision_id" db:"target_decision_id"`
	SourceAddress       ucxl.Address `json:"source_address" db:"source_address"`
	TargetAddress       ucxl.Address `json:"target_address" db:"target_address"`
	
	// Influence metrics
	InfluenceStrength   float64      `json:"influence_strength" db:"influence_strength"`
	InfluenceType       string       `json:"influence_type" db:"influence_type"` // direct, indirect, cascading
	PropagationDelay    float64      `json:"propagation_delay" db:"propagation_delay"` // hours
	HopDistance         int          `json:"hop_distance" db:"hop_distance"`
	
	// Path analysis
	ShortestPath        []byte       `json:"shortest_path" db:"shortest_path"` // JSON path array
	AlternatePaths      []byte       `json:"alternate_paths" db:"alternate_paths"` // JSON paths
	PathConfidence      float64      `json:"path_confidence" db:"path_confidence"`
	
	// Temporal tracking
	CreatedAt           time.Time    `json:"created_at" db:"created_at"`
	LastAnalyzedAt      time.Time    `json:"last_analyzed_at" db:"last_analyzed_at"`
	ValidatedAt         *time.Time   `json:"validated_at" db:"validated_at"`
}

// AccessControlRecord represents role-based access control metadata
type AccessControlRecord struct {
	ID              string                      `json:"id" db:"id"`
	UCXLAddress     ucxl.Address                `json:"ucxl_address" db:"ucxl_address"`
	Role            string                      `json:"role" db:"role"`
	Permissions     []byte                      `json:"permissions" db:"permissions"` // JSON permissions array
	
	// Access levels
	ReadAccess      bool                        `json:"read_access" db:"read_access"`
	WriteAccess     bool                        `json:"write_access" db:"write_access"`
	DeleteAccess    bool                        `json:"delete_access" db:"delete_access"`
	AdminAccess     bool                        `json:"admin_access" db:"admin_access"`
	AccessLevel     slurpContext.RoleAccessLevel `json:"access_level" db:"access_level"`
	
	// Constraints
	TimeConstraints []byte                      `json:"time_constraints" db:"time_constraints"` // JSON time rules
	IPConstraints   []byte                      `json:"ip_constraints" db:"ip_constraints"`     // JSON IP rules
	ContextFilters  []byte                      `json:"context_filters" db:"context_filters"`   // JSON filter rules
	
	// Audit trail
	CreatedAt       time.Time                   `json:"created_at" db:"created_at"`
	CreatedBy       string                      `json:"created_by" db:"created_by"`
	UpdatedAt       time.Time                   `json:"updated_at" db:"updated_at"`
	UpdatedBy       string                      `json:"updated_by" db:"updated_by"`
	ExpiresAt       *time.Time                  `json:"expires_at" db:"expires_at"`
}

// ContextIndexRecord represents search index entries for contexts
type ContextIndexRecord struct {
	ID              string       `json:"id" db:"id"`
	UCXLAddress     ucxl.Address `json:"ucxl_address" db:"ucxl_address"`
	IndexName       string       `json:"index_name" db:"index_name"`
	
	// Indexed content
	Tokens          []byte       `json:"tokens" db:"tokens"`         // JSON token array
	NGrams          []byte       `json:"ngrams" db:"ngrams"`         // JSON n-gram array
	SemanticVector  []byte       `json:"semantic_vector" db:"semantic_vector"` // Embedding vector
	
	// Search metadata
	IndexWeight     float64      `json:"index_weight" db:"index_weight"`
	BoostFactor     float64      `json:"boost_factor" db:"boost_factor"`
	Language        string       `json:"language" db:"language"`
	ContentType     string       `json:"content_type" db:"content_type"`
	
	// Quality metrics
	RelevanceScore  float64      `json:"relevance_score" db:"relevance_score"`
	FreshnessScore  float64      `json:"freshness_score" db:"freshness_score"`
	PopularityScore float64      `json:"popularity_score" db:"popularity_score"`
	
	// Temporal tracking
	CreatedAt       time.Time    `json:"created_at" db:"created_at"`
	UpdatedAt       time.Time    `json:"updated_at" db:"updated_at"`
	LastReindexed   time.Time    `json:"last_reindexed" db:"last_reindexed"`
}

// CacheEntryRecord represents cached context data
type CacheEntryRecord struct {
	ID              string       `json:"id" db:"id"`
	CacheKey        string       `json:"cache_key" db:"cache_key"`
	UCXLAddress     ucxl.Address `json:"ucxl_address" db:"ucxl_address"`
	Role            string       `json:"role" db:"role"`
	
	// Cached data
	CachedData      []byte       `json:"cached_data" db:"cached_data"`
	DataHash        string       `json:"data_hash" db:"data_hash"`
	Compressed      bool         `json:"compressed" db:"compressed"`
	OriginalSize    int64        `json:"original_size" db:"original_size"`
	CompressedSize  int64        `json:"compressed_size" db:"compressed_size"`
	
	// Cache metadata
	TTL             int64        `json:"ttl" db:"ttl"` // seconds
	Priority        int          `json:"priority" db:"priority"`
	AccessCount     int64        `json:"access_count" db:"access_count"`
	HitCount        int64        `json:"hit_count" db:"hit_count"`
	
	// Temporal data
	CreatedAt       time.Time    `json:"created_at" db:"created_at"`
	LastAccessedAt  time.Time    `json:"last_accessed_at" db:"last_accessed_at"`
	LastHitAt       *time.Time   `json:"last_hit_at" db:"last_hit_at"`
	ExpiresAt       time.Time    `json:"expires_at" db:"expires_at"`
}

// BackupRecord represents backup metadata
type BackupRecord struct {
	ID              string       `json:"id" db:"id"`
	BackupID        string       `json:"backup_id" db:"backup_id"`
	Name            string       `json:"name" db:"name"`
	Destination     string       `json:"destination" db:"destination"`
	
	// Backup content
	ContextCount    int64        `json:"context_count" db:"context_count"`
	DataSize        int64        `json:"data_size" db:"data_size"`
	CompressedSize  int64        `json:"compressed_size" db:"compressed_size"`
	Checksum        string       `json:"checksum" db:"checksum"`
	
	// Backup metadata
	IncludesIndexes bool         `json:"includes_indexes" db:"includes_indexes"`
	IncludesCache   bool         `json:"includes_cache" db:"includes_cache"`
	Encrypted       bool         `json:"encrypted" db:"encrypted"`
	Incremental     bool         `json:"incremental" db:"incremental"`
	ParentBackupID  string       `json:"parent_backup_id" db:"parent_backup_id"`
	
	// Status tracking
	Status          BackupStatus `json:"status" db:"status"`
	Progress        float64      `json:"progress" db:"progress"`
	ErrorMessage    string       `json:"error_message" db:"error_message"`
	
	// Temporal data
	CreatedAt       time.Time    `json:"created_at" db:"created_at"`
	StartedAt       *time.Time   `json:"started_at" db:"started_at"`
	CompletedAt     *time.Time   `json:"completed_at" db:"completed_at"`
	RetentionUntil  time.Time    `json:"retention_until" db:"retention_until"`
}

// MetricsRecord represents storage performance metrics
type MetricsRecord struct {
	ID              string       `json:"id" db:"id"`
	MetricType      string       `json:"metric_type" db:"metric_type"` // storage, encryption, cache, etc.
	NodeID          string       `json:"node_id" db:"node_id"`
	
	// Metric data
	MetricName      string       `json:"metric_name" db:"metric_name"`
	MetricValue     float64      `json:"metric_value" db:"metric_value"`
	MetricUnit      string       `json:"metric_unit" db:"metric_unit"`
	Tags            []byte       `json:"tags" db:"tags"` // JSON tag object
	
	// Aggregation data
	AggregationType string       `json:"aggregation_type" db:"aggregation_type"` // avg, sum, count, etc.
	TimeWindow      int64        `json:"time_window" db:"time_window"` // seconds
	SampleCount     int64        `json:"sample_count" db:"sample_count"`
	
	// Temporal tracking
	Timestamp       time.Time    `json:"timestamp" db:"timestamp"`
	CreatedAt       time.Time    `json:"created_at" db:"created_at"`
}

// ContextEvolutionRecord tracks how contexts evolve over time
type ContextEvolutionRecord struct {
	ID              string       `json:"id" db:"id"`
	UCXLAddress     ucxl.Address `json:"ucxl_address" db:"ucxl_address"`
	FromVersion     int64        `json:"from_version" db:"from_version"`
	ToVersion       int64        `json:"to_version" db:"to_version"`
	
	// Evolution analysis
	EvolutionType   string       `json:"evolution_type" db:"evolution_type"` // enhancement, refactor, fix, etc.
	SimilarityScore float64      `json:"similarity_score" db:"similarity_score"`
	ChangesMagnitude float64     `json:"changes_magnitude" db:"changes_magnitude"`
	SemanticDrift   float64      `json:"semantic_drift" db:"semantic_drift"`
	
	// Change details
	ChangedFields   []byte       `json:"changed_fields" db:"changed_fields"` // JSON array
	FieldDeltas     []byte       `json:"field_deltas" db:"field_deltas"`     // JSON delta object
	ImpactAnalysis  []byte       `json:"impact_analysis" db:"impact_analysis"` // JSON analysis
	
	// Quality assessment
	QualityImprovement float64   `json:"quality_improvement" db:"quality_improvement"`
	ConfidenceChange   float64   `json:"confidence_change" db:"confidence_change"`
	ValidationPassed   bool      `json:"validation_passed" db:"validation_passed"`
	
	// Temporal tracking
	EvolutionTime   time.Time    `json:"evolution_time" db:"evolution_time"`
	AnalyzedAt      time.Time    `json:"analyzed_at" db:"analyzed_at"`
	ProcessingTime  float64      `json:"processing_time" db:"processing_time"` // ms
}

// Schema validation and creation functions

// CreateTableStatements returns SQL DDL statements for creating all tables
func CreateTableStatements() []string {
	return []string{
		CreateContextTableSQL(),
		CreateEncryptedContextTableSQL(),
		CreateHierarchyTableSQL(),
		CreateDecisionHopTableSQL(),
		CreateDecisionInfluenceTableSQL(),
		CreateAccessControlTableSQL(),
		CreateContextIndexTableSQL(),
		CreateCacheEntryTableSQL(),
		CreateBackupTableSQL(),
		CreateMetricsTableSQL(),
		CreateEvolutionTableSQL(),
	}
}

// CreateIndexStatements returns SQL statements for creating indexes
func CreateIndexStatements() []string {
	return []string{
		// Context table indexes
		"CREATE INDEX IF NOT EXISTS idx_context_ucxl ON contexts(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_context_path_hash ON contexts(path_hash)",
		"CREATE INDEX IF NOT EXISTS idx_context_created_at ON contexts(created_at)",
		"CREATE INDEX IF NOT EXISTS idx_context_updated_at ON contexts(updated_at)",
		"CREATE INDEX IF NOT EXISTS idx_context_version ON contexts(version)",
		"CREATE INDEX IF NOT EXISTS idx_context_staleness ON contexts(staleness_score)",
		"CREATE INDEX IF NOT EXISTS idx_context_confidence ON contexts(rag_confidence)",
		
		// Encrypted context indexes
		"CREATE INDEX IF NOT EXISTS idx_encrypted_context_role ON encrypted_contexts(role)",
		"CREATE INDEX IF NOT EXISTS idx_encrypted_context_ucxl ON encrypted_contexts(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_encrypted_context_access_level ON encrypted_contexts(access_level)",
		"CREATE INDEX IF NOT EXISTS idx_encrypted_context_key_fp ON encrypted_contexts(key_fingerprint)",
		
		// Hierarchy indexes
		"CREATE INDEX IF NOT EXISTS idx_hierarchy_parent ON context_hierarchy(parent_address)",
		"CREATE INDEX IF NOT EXISTS idx_hierarchy_child ON context_hierarchy(child_address)",
		"CREATE INDEX IF NOT EXISTS idx_hierarchy_distance ON context_hierarchy(distance)",
		"CREATE INDEX IF NOT EXISTS idx_hierarchy_weight ON context_hierarchy(inheritance_weight)",
		
		// Decision hop indexes
		"CREATE INDEX IF NOT EXISTS idx_decision_ucxl ON decision_hops(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_decision_timestamp ON decision_hops(timestamp)",
		"CREATE INDEX IF NOT EXISTS idx_decision_reason ON decision_hops(change_reason)",
		"CREATE INDEX IF NOT EXISTS idx_decision_maker ON decision_hops(decision_maker)",
		"CREATE INDEX IF NOT EXISTS idx_decision_version ON decision_hops(context_version)",
		
		// Decision influence indexes
		"CREATE INDEX IF NOT EXISTS idx_influence_source ON decision_influence(source_decision_id)",
		"CREATE INDEX IF NOT EXISTS idx_influence_target ON decision_influence(target_decision_id)",
		"CREATE INDEX IF NOT EXISTS idx_influence_strength ON decision_influence(influence_strength)",
		"CREATE INDEX IF NOT EXISTS idx_influence_hop_distance ON decision_influence(hop_distance)",
		
		// Access control indexes
		"CREATE INDEX IF NOT EXISTS idx_access_role ON access_control(role)",
		"CREATE INDEX IF NOT EXISTS idx_access_ucxl ON access_control(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_access_level ON access_control(access_level)",
		"CREATE INDEX IF NOT EXISTS idx_access_expires ON access_control(expires_at)",
		
		// Search index indexes
		"CREATE INDEX IF NOT EXISTS idx_context_index_name ON context_indexes(index_name)",
		"CREATE INDEX IF NOT EXISTS idx_context_index_ucxl ON context_indexes(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_context_index_relevance ON context_indexes(relevance_score)",
		"CREATE INDEX IF NOT EXISTS idx_context_index_freshness ON context_indexes(freshness_score)",
		
		// Cache indexes
		"CREATE INDEX IF NOT EXISTS idx_cache_key ON cache_entries(cache_key)",
		"CREATE INDEX IF NOT EXISTS idx_cache_ucxl ON cache_entries(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_cache_role ON cache_entries(role)",
		"CREATE INDEX IF NOT EXISTS idx_cache_expires ON cache_entries(expires_at)",
		"CREATE INDEX IF NOT EXISTS idx_cache_priority ON cache_entries(priority)",
		"CREATE INDEX IF NOT EXISTS idx_cache_access_count ON cache_entries(access_count)",
		
		// Metrics indexes
		"CREATE INDEX IF NOT EXISTS idx_metrics_type ON metrics(metric_type)",
		"CREATE INDEX IF NOT EXISTS idx_metrics_name ON metrics(metric_name)",
		"CREATE INDEX IF NOT EXISTS idx_metrics_node ON metrics(node_id)",
		"CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON metrics(timestamp)",
		
		// Evolution indexes
		"CREATE INDEX IF NOT EXISTS idx_evolution_ucxl ON context_evolution(ucxl_address)",
		"CREATE INDEX IF NOT EXISTS idx_evolution_from_version ON context_evolution(from_version)",
		"CREATE INDEX IF NOT EXISTS idx_evolution_to_version ON context_evolution(to_version)",
		"CREATE INDEX IF NOT EXISTS idx_evolution_time ON context_evolution(evolution_time)",
		"CREATE INDEX IF NOT EXISTS idx_evolution_type ON context_evolution(evolution_type)",
	}
}

// Individual table creation SQL statements
// These would be implemented with specific SQL DDL for the chosen database
// For now, providing the structure - actual SQL would depend on database choice

func CreateContextTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS contexts (
		id TEXT PRIMARY KEY,
		ucxl_address TEXT NOT NULL,
		path TEXT NOT NULL,
		path_hash TEXT NOT NULL,
		summary TEXT,
		purpose TEXT,
		technologies BLOB,
		tags BLOB,
		insights BLOB,
		overrides_parent BOOLEAN DEFAULT FALSE,
		context_specificity INTEGER DEFAULT 0,
		applies_to_children BOOLEAN DEFAULT TRUE,
		rag_confidence REAL DEFAULT 0.0,
		staleness_score REAL DEFAULT 0.0,
		validation_score REAL DEFAULT 0.0,
		version INTEGER NOT NULL DEFAULT 1,
		parent_version INTEGER,
		context_hash TEXT NOT NULL,
		created_at DATETIME NOT NULL,
		updated_at DATETIME NOT NULL,
		generated_at DATETIME NOT NULL,
		last_accessed_at DATETIME,
		expires_at DATETIME,
		storage_type TEXT DEFAULT 'local',
		compression_type TEXT DEFAULT 'none',
		encryption_level INTEGER DEFAULT 0,
		replication_factor INTEGER DEFAULT 1,
		checksum TEXT NOT NULL,
		data_size INTEGER DEFAULT 0,
		compressed_size INTEGER DEFAULT 0
	)`
}

func CreateEncryptedContextTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS encrypted_contexts (
		id TEXT PRIMARY KEY,
		context_id TEXT NOT NULL,
		role TEXT NOT NULL,
		ucxl_address TEXT NOT NULL,
		access_level INTEGER NOT NULL,
		encrypted_data BLOB NOT NULL,
		key_fingerprint TEXT NOT NULL,
		encryption_algo TEXT NOT NULL,
		key_version INTEGER DEFAULT 1,
		data_checksum TEXT NOT NULL,
		encryption_hash TEXT NOT NULL,
		created_at DATETIME NOT NULL,
		updated_at DATETIME NOT NULL,
		last_decrypted_at DATETIME,
		expires_at DATETIME,
		access_count INTEGER DEFAULT 0,
		last_accessed_by TEXT,
		access_history BLOB,
		FOREIGN KEY (context_id) REFERENCES contexts(id) ON DELETE CASCADE,
		UNIQUE(context_id, role)
	)`
}

func CreateHierarchyTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS context_hierarchy (
		id TEXT PRIMARY KEY,
		parent_address TEXT NOT NULL,
		child_address TEXT NOT NULL,
		parent_path TEXT NOT NULL,
		child_path TEXT NOT NULL,
		relationship_type TEXT NOT NULL,
		inheritance_weight REAL DEFAULT 1.0,
		override_strength INTEGER DEFAULT 0,
		distance INTEGER NOT NULL,
		created_at DATETIME NOT NULL,
		validated_at DATETIME NOT NULL,
		last_resolved_at DATETIME,
		resolution_count INTEGER DEFAULT 0,
		resolution_time REAL DEFAULT 0.0,
		UNIQUE(parent_address, child_address)
	)`
}

func CreateDecisionHopTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS decision_hops (
		id TEXT PRIMARY KEY,
		decision_id TEXT NOT NULL,
		ucxl_address TEXT NOT NULL,
		context_version INTEGER NOT NULL,
		change_reason TEXT NOT NULL,
		decision_maker TEXT NOT NULL,
		decision_rationale TEXT,
		impact_scope TEXT NOT NULL,
		confidence_level REAL DEFAULT 0.0,
		previous_hash TEXT,
		current_hash TEXT NOT NULL,
		context_delta BLOB,
		staleness_score REAL DEFAULT 0.0,
		timestamp DATETIME NOT NULL,
		previous_decision_time DATETIME,
		processing_time REAL DEFAULT 0.0,
		external_refs BLOB,
		commit_hash TEXT,
		ticket_id TEXT
	)`
}

func CreateDecisionInfluenceTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS decision_influence (
		id TEXT PRIMARY KEY,
		source_decision_id TEXT NOT NULL,
		target_decision_id TEXT NOT NULL,
		source_address TEXT NOT NULL,
		target_address TEXT NOT NULL,
		influence_strength REAL NOT NULL,
		influence_type TEXT NOT NULL,
		propagation_delay REAL DEFAULT 0.0,
		hop_distance INTEGER NOT NULL,
		shortest_path BLOB,
		alternate_paths BLOB,
		path_confidence REAL DEFAULT 0.0,
		created_at DATETIME NOT NULL,
		last_analyzed_at DATETIME NOT NULL,
		validated_at DATETIME
	)`
}

func CreateAccessControlTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS access_control (
		id TEXT PRIMARY KEY,
		ucxl_address TEXT NOT NULL,
		role TEXT NOT NULL,
		permissions BLOB,
		read_access BOOLEAN DEFAULT FALSE,
		write_access BOOLEAN DEFAULT FALSE,
		delete_access BOOLEAN DEFAULT FALSE,
		admin_access BOOLEAN DEFAULT FALSE,
		access_level INTEGER NOT NULL,
		time_constraints BLOB,
		ip_constraints BLOB,
		context_filters BLOB,
		created_at DATETIME NOT NULL,
		created_by TEXT NOT NULL,
		updated_at DATETIME NOT NULL,
		updated_by TEXT NOT NULL,
		expires_at DATETIME,
		UNIQUE(ucxl_address, role)
	)`
}

func CreateContextIndexTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS context_indexes (
		id TEXT PRIMARY KEY,
		ucxl_address TEXT NOT NULL,
		index_name TEXT NOT NULL,
		tokens BLOB,
		ngrams BLOB,
		semantic_vector BLOB,
		index_weight REAL DEFAULT 1.0,
		boost_factor REAL DEFAULT 1.0,
		language TEXT DEFAULT 'en',
		content_type TEXT,
		relevance_score REAL DEFAULT 0.0,
		freshness_score REAL DEFAULT 0.0,
		popularity_score REAL DEFAULT 0.0,
		created_at DATETIME NOT NULL,
		updated_at DATETIME NOT NULL,
		last_reindexed DATETIME NOT NULL,
		UNIQUE(ucxl_address, index_name)
	)`
}

func CreateCacheEntryTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS cache_entries (
		id TEXT PRIMARY KEY,
		cache_key TEXT NOT NULL UNIQUE,
		ucxl_address TEXT NOT NULL,
		role TEXT NOT NULL,
		cached_data BLOB NOT NULL,
		data_hash TEXT NOT NULL,
		compressed BOOLEAN DEFAULT FALSE,
		original_size INTEGER DEFAULT 0,
		compressed_size INTEGER DEFAULT 0,
		ttl INTEGER NOT NULL,
		priority INTEGER DEFAULT 0,
		access_count INTEGER DEFAULT 0,
		hit_count INTEGER DEFAULT 0,
		created_at DATETIME NOT NULL,
		last_accessed_at DATETIME NOT NULL,
		last_hit_at DATETIME,
		expires_at DATETIME NOT NULL
	)`
}

func CreateBackupTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS backups (
		id TEXT PRIMARY KEY,
		backup_id TEXT NOT NULL UNIQUE,
		name TEXT NOT NULL,
		destination TEXT NOT NULL,
		context_count INTEGER DEFAULT 0,
		data_size INTEGER DEFAULT 0,
		compressed_size INTEGER DEFAULT 0,
		checksum TEXT NOT NULL,
		includes_indexes BOOLEAN DEFAULT FALSE,
		includes_cache BOOLEAN DEFAULT FALSE,
		encrypted BOOLEAN DEFAULT FALSE,
		incremental BOOLEAN DEFAULT FALSE,
		parent_backup_id TEXT,
		status TEXT NOT NULL,
		progress REAL DEFAULT 0.0,
		error_message TEXT,
		created_at DATETIME NOT NULL,
		started_at DATETIME,
		completed_at DATETIME,
		retention_until DATETIME NOT NULL
	)`
}

func CreateMetricsTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS metrics (
		id TEXT PRIMARY KEY,
		metric_type TEXT NOT NULL,
		node_id TEXT NOT NULL,
		metric_name TEXT NOT NULL,
		metric_value REAL NOT NULL,
		metric_unit TEXT NOT NULL,
		tags BLOB,
		aggregation_type TEXT DEFAULT 'instant',
		time_window INTEGER DEFAULT 0,
		sample_count INTEGER DEFAULT 1,
		timestamp DATETIME NOT NULL,
		created_at DATETIME NOT NULL
	)`
}

func CreateEvolutionTableSQL() string {
	return `CREATE TABLE IF NOT EXISTS context_evolution (
		id TEXT PRIMARY KEY,
		ucxl_address TEXT NOT NULL,
		from_version INTEGER NOT NULL,
		to_version INTEGER NOT NULL,
		evolution_type TEXT NOT NULL,
		similarity_score REAL DEFAULT 0.0,
		changes_magnitude REAL DEFAULT 0.0,
		semantic_drift REAL DEFAULT 0.0,
		changed_fields BLOB,
		field_deltas BLOB,
		impact_analysis BLOB,
		quality_improvement REAL DEFAULT 0.0,
		confidence_change REAL DEFAULT 0.0,
		validation_passed BOOLEAN DEFAULT FALSE,
		evolution_time DATETIME NOT NULL,
		analyzed_at DATETIME NOT NULL,
		processing_time REAL DEFAULT 0.0,
		UNIQUE(ucxl_address, from_version, to_version)
	)`
}
