package storage

import (
	"time"

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

// ListCriteria represents criteria for listing contexts
type ListCriteria struct {
	// Filter criteria
	Tags         []string          `json:"tags"`          // Required tags
	Technologies []string          `json:"technologies"`  // Required technologies
	Roles        []string          `json:"roles"`         // Accessible roles
	PathPattern  string            `json:"path_pattern"`  // Path pattern to match
	
	// Date filters
	CreatedAfter  *time.Time       `json:"created_after,omitempty"`  // Created after date
	CreatedBefore *time.Time       `json:"created_before,omitempty"` // Created before date
	UpdatedAfter  *time.Time       `json:"updated_after,omitempty"`  // Updated after date
	UpdatedBefore *time.Time       `json:"updated_before,omitempty"` // Updated before date
	
	// Quality filters
	MinConfidence float64          `json:"min_confidence"` // Minimum confidence score
	MaxAge        *time.Duration   `json:"max_age,omitempty"` // Maximum age
	
	// Pagination
	Offset        int              `json:"offset"`        // Result offset
	Limit         int              `json:"limit"`         // Maximum results
	
	// Sorting
	SortBy        string           `json:"sort_by"`       // Sort field
	SortOrder     string           `json:"sort_order"`    // Sort order (asc, desc)
	
	// Options
	IncludeStale  bool             `json:"include_stale"` // Include stale contexts
}

// SearchQuery represents a search query for contexts
type SearchQuery struct {
	// Query terms
	Query          string           `json:"query"`           // Main search query
	Tags           []string         `json:"tags"`            // Required tags
	Technologies   []string         `json:"technologies"`    // Required technologies
	FileTypes      []string         `json:"file_types"`      // File types to include
	
	// Filters
	MinConfidence  float64          `json:"min_confidence"`  // Minimum confidence
	MaxAge         *time.Duration   `json:"max_age"`         // Maximum age
	Roles          []string         `json:"roles"`           // Required access roles
	
	// Scope
	Scope          []string         `json:"scope"`           // Paths to search within
	ExcludeScope   []string         `json:"exclude_scope"`   // Paths to exclude
	
	// Result options
	Limit          int              `json:"limit"`           // Maximum results
	Offset         int              `json:"offset"`          // Result offset
	SortBy         string           `json:"sort_by"`         // Sort field
	SortOrder      string           `json:"sort_order"`      // asc, desc
	
	// Advanced options
	FuzzyMatch     bool             `json:"fuzzy_match"`     // Enable fuzzy matching
	IncludeStale   bool             `json:"include_stale"`   // Include stale contexts
	HighlightTerms bool             `json:"highlight_terms"` // Highlight search terms
	
	// Faceted search
	Facets         []string         `json:"facets"`          // Facets to include
	FacetFilters   map[string][]string `json:"facet_filters"` // Facet filters
}

// SearchResults represents search query results
type SearchResults struct {
	Query          *SearchQuery     `json:"query"`           // Original query
	Results        []*SearchResult  `json:"results"`         // Search results
	TotalResults   int64            `json:"total_results"`   // Total matching results
	ProcessingTime time.Duration    `json:"processing_time"` // Query processing time
	Facets         map[string]map[string]int `json:"facets"` // Faceted results
	Suggestions    []string         `json:"suggestions"`     // Query suggestions
	ProcessedAt    time.Time        `json:"processed_at"`    // When query was processed
}

// SearchResult represents a single search result
type SearchResult struct {
	Context       *slurpContext.ContextNode `json:"context"`        // Context data
	MatchScore    float64                   `json:"match_score"`    // Match relevance score
	MatchedFields []string                  `json:"matched_fields"` // Fields that matched
	Highlights    map[string][]string       `json:"highlights"`     // Highlighted text snippets
	Explanation   string                    `json:"explanation"`    // Match explanation
	Rank          int                       `json:"rank"`           // Result ranking
}

// BatchStoreRequest represents a batch store operation
type BatchStoreRequest struct {
	Contexts      []*ContextStoreItem `json:"contexts"`       // Contexts to store
	Roles         []string            `json:"roles"`          // Default roles for all contexts
	Options       *StoreOptions       `json:"options"`        // Store options
	Transaction   bool                `json:"transaction"`    // Use transaction
	FailOnError   bool                `json:"fail_on_error"`  // Fail entire batch on error
}

// ContextStoreItem represents a single item in batch store
type ContextStoreItem struct {
	Context   *slurpContext.ContextNode `json:"context"`   // Context to store
	Roles     []string                  `json:"roles"`     // Specific roles (overrides default)
	Options   *StoreOptions             `json:"options"`   // Item-specific options
}

// BatchStoreResult represents the result of batch store operation
type BatchStoreResult struct {
	SuccessCount  int                     `json:"success_count"`  // Number of successful stores
	ErrorCount    int                     `json:"error_count"`    // Number of failed stores
	Errors        map[string]error        `json:"errors"`         // Errors by context path
	ProcessingTime time.Duration          `json:"processing_time"` // Total processing time
	ProcessedAt   time.Time               `json:"processed_at"`   // When batch was processed
}

// BatchRetrieveRequest represents a batch retrieve operation
type BatchRetrieveRequest struct {
	Addresses     []ucxl.Address `json:"addresses"`      // Addresses to retrieve
	Role          string         `json:"role"`           // Role for access control
	Options       *RetrieveOptions `json:"options"`      // Retrieve options
	FailOnError   bool           `json:"fail_on_error"`  // Fail entire batch on error
}

// BatchRetrieveResult represents the result of batch retrieve operation
type BatchRetrieveResult struct {
	Contexts      map[string]*slurpContext.ContextNode `json:"contexts"`       // Retrieved contexts by address
	SuccessCount  int                                  `json:"success_count"`  // Number of successful retrieves
	ErrorCount    int                                  `json:"error_count"`    // Number of failed retrieves
	Errors        map[string]error                     `json:"errors"`         // Errors by address
	ProcessingTime time.Duration                       `json:"processing_time"` // Total processing time
	ProcessedAt   time.Time                           `json:"processed_at"`   // When batch was processed
}

// StoreOptions represents options for storing contexts
type StoreOptions struct {
	Encrypt       bool              `json:"encrypt"`        // Whether to encrypt data
	Replicate     bool              `json:"replicate"`      // Whether to replicate across nodes
	Index         bool              `json:"index"`          // Whether to add to search index
	Cache         bool              `json:"cache"`          // Whether to cache locally
	Compress      bool              `json:"compress"`       // Whether to compress data
	TTL           *time.Duration    `json:"ttl,omitempty"`  // Time to live
	AccessLevel   crypto.AccessLevel `json:"access_level"`  // Required access level
	Metadata      map[string]interface{} `json:"metadata"` // Additional metadata
}

// RetrieveOptions represents options for retrieving contexts
type RetrieveOptions struct {
	UseCache      bool              `json:"use_cache"`      // Whether to use cache
	RefreshCache  bool              `json:"refresh_cache"`  // Whether to refresh cache
	IncludeStale  bool              `json:"include_stale"`  // Include stale contexts
	MaxAge        *time.Duration    `json:"max_age,omitempty"` // Maximum acceptable age
	Decompress    bool              `json:"decompress"`     // Whether to decompress data
	ValidateIntegrity bool          `json:"validate_integrity"` // Validate data integrity
}

// DistributedStoreOptions represents options for distributed storage
type DistributedStoreOptions struct {
	ReplicationFactor int               `json:"replication_factor"` // Number of replicas
	ConsistencyLevel  ConsistencyLevel  `json:"consistency_level"`  // Consistency requirements
	Timeout           time.Duration     `json:"timeout"`            // Operation timeout
	PreferLocal       bool              `json:"prefer_local"`       // Prefer local storage
	SyncMode          SyncMode          `json:"sync_mode"`          // Synchronization mode
}

// ConsistencyLevel represents consistency requirements
type ConsistencyLevel string

const (
	ConsistencyEventual ConsistencyLevel = "eventual" // Eventual consistency
	ConsistencyStrong   ConsistencyLevel = "strong"   // Strong consistency
	ConsistencyQuorum   ConsistencyLevel = "quorum"   // Quorum-based consistency
)

// SyncMode represents synchronization mode
type SyncMode string

const (
	SyncAsync  SyncMode = "async"  // Asynchronous synchronization
	SyncSync   SyncMode = "sync"   // Synchronous synchronization
	SyncLazy   SyncMode = "lazy"   // Lazy synchronization
)

// StorageStatistics represents overall storage statistics
type StorageStatistics struct {
	TotalContexts     int64         `json:"total_contexts"`      // Total stored contexts
	LocalContexts     int64         `json:"local_contexts"`      // Locally stored contexts
	DistributedContexts int64       `json:"distributed_contexts"` // Distributed contexts
	TotalSize         int64         `json:"total_size"`          // Total storage size
	CompressedSize    int64         `json:"compressed_size"`     // Compressed storage size
	IndexSize         int64         `json:"index_size"`          // Search index size
	CacheSize         int64         `json:"cache_size"`          // Cache size
	ReplicationFactor float64       `json:"replication_factor"`  // Average replication factor
	AvailableSpace    int64         `json:"available_space"`     // Available storage space
	LastSyncTime      time.Time     `json:"last_sync_time"`      // Last synchronization
	SyncErrors        int64         `json:"sync_errors"`         // Synchronization errors
	OperationsPerSecond float64     `json:"operations_per_second"` // Operations per second
	AverageLatency    time.Duration `json:"average_latency"`     // Average operation latency
}

// LocalStorageStats represents local storage statistics
type LocalStorageStats struct {
	TotalFiles        int64         `json:"total_files"`         // Total stored files
	TotalSize         int64         `json:"total_size"`          // Total storage size
	CompressedSize    int64         `json:"compressed_size"`     // Compressed size
	AvailableSpace    int64         `json:"available_space"`     // Available disk space
	FragmentationRatio float64      `json:"fragmentation_ratio"` // Storage fragmentation
	LastCompaction    time.Time     `json:"last_compaction"`     // Last compaction time
	ReadOperations    int64         `json:"read_operations"`     // Read operations count
	WriteOperations   int64         `json:"write_operations"`    // Write operations count
	AverageReadTime   time.Duration `json:"average_read_time"`   // Average read time
	AverageWriteTime  time.Duration `json:"average_write_time"`  // Average write time
}

// DistributedStorageStats represents distributed storage statistics
type DistributedStorageStats struct {
	TotalNodes        int           `json:"total_nodes"`         // Total nodes in cluster
	ActiveNodes       int           `json:"active_nodes"`        // Active nodes
	FailedNodes       int           `json:"failed_nodes"`        // Failed nodes
	TotalReplicas     int64         `json:"total_replicas"`      // Total replicas
	HealthyReplicas   int64         `json:"healthy_replicas"`    // Healthy replicas
	UnderReplicated   int64         `json:"under_replicated"`    // Under-replicated data
	NetworkLatency    time.Duration `json:"network_latency"`     // Average network latency
	ReplicationLatency time.Duration `json:"replication_latency"` // Average replication latency
	ConsensusTime     time.Duration `json:"consensus_time"`      // Average consensus time
	LastRebalance     time.Time     `json:"last_rebalance"`      // Last rebalance operation
}

// CacheStatistics represents cache performance statistics
type CacheStatistics struct {
	HitRate           float64       `json:"hit_rate"`            // Cache hit rate
	MissRate          float64       `json:"miss_rate"`           // Cache miss rate
	TotalHits         int64         `json:"total_hits"`          // Total cache hits
	TotalMisses       int64         `json:"total_misses"`        // Total cache misses
	CurrentSize       int64         `json:"current_size"`        // Current cache size
	MaxSize           int64         `json:"max_size"`            // Maximum cache size
	EvictionCount     int64         `json:"eviction_count"`      // Number of evictions
	AverageLoadTime   time.Duration `json:"average_load_time"`   // Average cache load time
	LastEviction      time.Time     `json:"last_eviction"`       // Last eviction time
	MemoryUsage       int64         `json:"memory_usage"`        // Memory usage in bytes
}

// CachePolicy represents caching policy configuration
type CachePolicy struct {
	TTL               time.Duration `json:"ttl"`                // Default TTL
	MaxSize           int64         `json:"max_size"`           // Maximum cache size
	EvictionPolicy    string        `json:"eviction_policy"`    // Eviction policy (LRU, LFU, etc.)
	RefreshThreshold  float64       `json:"refresh_threshold"`  // Refresh threshold
	WarmupEnabled     bool          `json:"warmup_enabled"`     // Enable cache warmup
	CompressEntries   bool          `json:"compress_entries"`   // Compress cache entries
	MaxEntrySize      int64         `json:"max_entry_size"`     // Maximum entry size
}

// IndexConfig represents search index configuration
type IndexConfig struct {
	Name              string            `json:"name"`               // Index name
	Fields            []string          `json:"fields"`             // Indexed fields
	Analyzer          string            `json:"analyzer"`           // Text analyzer
	Language          string            `json:"language"`           // Index language
	CaseSensitive     bool              `json:"case_sensitive"`     // Case sensitivity
	Stemming          bool              `json:"stemming"`           // Enable stemming
	StopWords         []string          `json:"stop_words"`         // Stop words list
	Synonyms          map[string][]string `json:"synonyms"`         // Synonym mappings
	MaxDocumentSize   int64             `json:"max_document_size"`  // Max document size
	RefreshInterval   time.Duration     `json:"refresh_interval"`   // Index refresh interval
}

// IndexStatistics represents search index statistics
type IndexStatistics struct {
	Name              string        `json:"name"`               // Index name
	DocumentCount     int64         `json:"document_count"`     // Total documents
	IndexSize         int64         `json:"index_size"`         // Index size in bytes
	LastUpdate        time.Time     `json:"last_update"`        // Last update time
	QueryCount        int64         `json:"query_count"`        // Total queries
	AverageQueryTime  time.Duration `json:"average_query_time"` // Average query time
	SuccessRate       float64       `json:"success_rate"`       // Query success rate
	FragmentationRatio float64      `json:"fragmentation_ratio"` // Index fragmentation
	LastOptimization  time.Time     `json:"last_optimization"`  // Last optimization time
}

// BackupConfig represents backup configuration
type BackupConfig struct {
	Name              string            `json:"name"`               // Backup name
	Destination       string            `json:"destination"`        // Backup destination
	IncludeIndexes    bool              `json:"include_indexes"`    // Include search indexes
	IncludeCache      bool              `json:"include_cache"`      // Include cache data
	Compression       bool              `json:"compression"`        // Enable compression
	Encryption        bool              `json:"encryption"`         // Enable encryption
	EncryptionKey     string            `json:"encryption_key"`     // Encryption key
	Incremental       bool              `json:"incremental"`        // Incremental backup
	Retention         time.Duration     `json:"retention"`          // Backup retention period
	Metadata          map[string]interface{} `json:"metadata"`      // Additional metadata
}

// BackupInfo represents information about a backup
type BackupInfo struct {
	ID                string            `json:"id"`                 // Backup ID
	Name              string            `json:"name"`               // Backup name
	CreatedAt         time.Time         `json:"created_at"`         // Creation time
	Size              int64             `json:"size"`               // Backup size
	CompressedSize    int64             `json:"compressed_size"`    // Compressed size
	ContextCount      int64             `json:"context_count"`      // Number of contexts
	Encrypted         bool              `json:"encrypted"`          // Whether encrypted
	Incremental       bool              `json:"incremental"`        // Whether incremental
	ParentBackupID    string            `json:"parent_backup_id"`   // Parent backup for incremental
	Checksum          string            `json:"checksum"`           // Backup checksum
	Status            BackupStatus      `json:"status"`             // Backup status
	Metadata          map[string]interface{} `json:"metadata"`      // Additional metadata
}

// BackupStatus represents backup status
type BackupStatus string

const (
	BackupInProgress BackupStatus = "in_progress"
	BackupCompleted  BackupStatus = "completed"
	BackupFailed     BackupStatus = "failed"
	BackupCorrupted  BackupStatus = "corrupted"
)

// RestoreConfig represents restore configuration
type RestoreConfig struct {
	BackupID          string            `json:"backup_id"`          // Backup to restore from
	Destination       string            `json:"destination"`        // Restore destination
	OverwriteExisting bool              `json:"overwrite_existing"` // Overwrite existing data
	RestoreIndexes    bool              `json:"restore_indexes"`    // Restore search indexes
	RestoreCache      bool              `json:"restore_cache"`      // Restore cache data
	ValidateIntegrity bool              `json:"validate_integrity"` // Validate data integrity
	DecryptionKey     string            `json:"decryption_key"`     // Decryption key
	Metadata          map[string]interface{} `json:"metadata"`      // Additional metadata
}

// BackupValidation represents backup validation results
type BackupValidation struct {
	BackupID          string            `json:"backup_id"`          // Backup ID
	Valid             bool              `json:"valid"`              // Whether backup is valid
	ChecksumMatch     bool              `json:"checksum_match"`     // Whether checksum matches
	CorruptedFiles    []string          `json:"corrupted_files"`    // List of corrupted files
	MissingFiles      []string          `json:"missing_files"`      // List of missing files
	ValidationTime    time.Duration     `json:"validation_time"`    // Validation duration
	ValidatedAt       time.Time         `json:"validated_at"`       // When validated
	ErrorCount        int               `json:"error_count"`        // Number of errors
	WarningCount      int               `json:"warning_count"`      // Number of warnings
}

// BackupSchedule represents automatic backup scheduling
type BackupSchedule struct {
	ID                string            `json:"id"`                 // Schedule ID
	Name              string            `json:"name"`               // Schedule name
	Cron              string            `json:"cron"`               // Cron expression
	BackupConfig      *BackupConfig     `json:"backup_config"`      // Backup configuration
	Enabled           bool              `json:"enabled"`            // Whether schedule is enabled
	LastRun           *time.Time        `json:"last_run,omitempty"` // Last execution time
	NextRun           *time.Time        `json:"next_run,omitempty"` // Next scheduled execution
	ConsecutiveFailures int             `json:"consecutive_failures"` // Consecutive failure count
	MaxFailures       int               `json:"max_failures"`       // Max allowed failures
}

// BackupStatistics represents backup statistics
type BackupStatistics struct {
	TotalBackups      int64         `json:"total_backups"`       // Total backups created
	SuccessfulBackups int64         `json:"successful_backups"`  // Successful backups
	FailedBackups     int64         `json:"failed_backups"`      // Failed backups
	TotalBackupSize   int64         `json:"total_backup_size"`   // Total backup storage size
	AverageBackupTime time.Duration `json:"average_backup_time"` // Average backup time
	LastBackupTime    time.Time     `json:"last_backup_time"`    // Last backup time
	OldestBackup      time.Time     `json:"oldest_backup"`       // Oldest backup time
	CompressionRatio  float64       `json:"compression_ratio"`   // Average compression ratio
	EncryptionEnabled bool          `json:"encryption_enabled"`  // Whether encryption is enabled
}