package intelligence

import (
	"context"
	"time"

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

// IntelligenceEngine provides AI-powered context analysis and generation
//
// The engine analyzes filesystem content, code structures, and project patterns
// to generate comprehensive contextual understanding. It integrates with RAG
// systems and applies role-specific analysis for enhanced context quality.
type IntelligenceEngine interface {
	// AnalyzeFile analyzes a single file and generates context
	// Performs content analysis, language detection, and pattern recognition
	AnalyzeFile(ctx context.Context, filePath string, role string) (*slurpContext.ContextNode, error)
	
	// AnalyzeDirectory analyzes directory structure for hierarchical patterns
	// Identifies organizational patterns, naming conventions, and structure insights
	AnalyzeDirectory(ctx context.Context, dirPath string) ([]*slurpContext.ContextNode, error)
	
	// GenerateRoleInsights generates role-specific insights for existing context
	// Provides specialized analysis based on role requirements and perspectives
	GenerateRoleInsights(ctx context.Context, baseContext *slurpContext.ContextNode, role string) ([]string, error)
	
	// AssessGoalAlignment assesses how well context aligns with project goals
	// Returns alignment score and specific alignment metrics
	AssessGoalAlignment(ctx context.Context, node *slurpContext.ContextNode) (float64, error)
	
	// AnalyzeBatch processes multiple files efficiently in parallel
	// Optimized for bulk analysis operations with resource management
	AnalyzeBatch(ctx context.Context, filePaths []string, role string) (map[string]*slurpContext.ContextNode, error)
	
	// DetectPatterns identifies recurring patterns across multiple contexts
	// Useful for template creation and standardization
	DetectPatterns(ctx context.Context, contexts []*slurpContext.ContextNode) ([]*Pattern, error)
	
	// EnhanceWithRAG enhances context using RAG system knowledge
	// Integrates external knowledge for richer context understanding
	EnhanceWithRAG(ctx context.Context, node *slurpContext.ContextNode) (*slurpContext.ContextNode, error)
	
	// ValidateContext validates generated context quality and consistency
	// Ensures context meets quality thresholds and consistency requirements
	ValidateContext(ctx context.Context, node *slurpContext.ContextNode) (*ValidationResult, error)
	
	// GetEngineStats returns engine performance and operational statistics
	GetEngineStats() (*EngineStatistics, error)
	
	// SetConfiguration updates engine configuration
	SetConfiguration(config *EngineConfig) error
}

// FileAnalyzer handles analysis of individual files
type FileAnalyzer interface {
	// AnalyzeContent analyzes file content for context extraction
	AnalyzeContent(ctx context.Context, filePath string, content []byte) (*FileAnalysis, error)
	
	// DetectLanguage detects programming language from content
	DetectLanguage(ctx context.Context, filePath string, content []byte) (string, float64, error)
	
	// ExtractMetadata extracts file metadata and statistics
	ExtractMetadata(ctx context.Context, filePath string) (*FileMetadata, error)
	
	// AnalyzeStructure analyzes code structure and organization
	AnalyzeStructure(ctx context.Context, filePath string, content []byte) (*StructureAnalysis, error)
	
	// IdentifyPurpose identifies the primary purpose of the file
	IdentifyPurpose(ctx context.Context, analysis *FileAnalysis) (string, float64, error)
	
	// GenerateSummary generates a concise summary of file content
	GenerateSummary(ctx context.Context, analysis *FileAnalysis) (string, error)
	
	// ExtractTechnologies identifies technologies used in the file
	ExtractTechnologies(ctx context.Context, analysis *FileAnalysis) ([]string, error)
}

// DirectoryAnalyzer handles analysis of directory structures
type DirectoryAnalyzer interface {
	// AnalyzeStructure analyzes directory organization patterns
	AnalyzeStructure(ctx context.Context, dirPath string) (*DirectoryStructure, error)
	
	// DetectConventions identifies naming and organizational conventions
	DetectConventions(ctx context.Context, dirPath string) (*ConventionAnalysis, error)
	
	// IdentifyPurpose determines the primary purpose of a directory
	IdentifyPurpose(ctx context.Context, structure *DirectoryStructure) (string, float64, error)
	
	// AnalyzeRelationships analyzes relationships between subdirectories
	AnalyzeRelationships(ctx context.Context, dirPath string) (*RelationshipAnalysis, error)
	
	// GenerateHierarchy generates context hierarchy for directory tree
	GenerateHierarchy(ctx context.Context, rootPath string, maxDepth int) ([]*slurpContext.ContextNode, error)
}

// PatternDetector identifies patterns in code and context
type PatternDetector interface {
	// DetectCodePatterns identifies code patterns and architectural styles
	DetectCodePatterns(ctx context.Context, filePath string, content []byte) ([]*CodePattern, error)
	
	// DetectNamingPatterns identifies naming conventions and patterns
	DetectNamingPatterns(ctx context.Context, contexts []*slurpContext.ContextNode) ([]*NamingPattern, error)
	
	// DetectOrganizationalPatterns identifies organizational patterns
	DetectOrganizationalPatterns(ctx context.Context, rootPath string) ([]*OrganizationalPattern, error)
	
	// MatchPatterns matches context against known patterns
	MatchPatterns(ctx context.Context, node *slurpContext.ContextNode, patterns []*Pattern) ([]*PatternMatch, error)
	
	// LearnPatterns learns new patterns from context examples
	LearnPatterns(ctx context.Context, examples []*slurpContext.ContextNode) ([]*Pattern, error)
}

// RAGIntegration handles integration with RAG systems
type RAGIntegration interface {
	// Query queries the RAG system for relevant information
	Query(ctx context.Context, query string, context map[string]interface{}) (*RAGResponse, error)
	
	// EnhanceContext enhances context using RAG knowledge
	EnhanceContext(ctx context.Context, node *slurpContext.ContextNode) (*slurpContext.ContextNode, error)
	
	// IndexContent indexes content for RAG retrieval
	IndexContent(ctx context.Context, content string, metadata map[string]interface{}) error
	
	// SearchSimilar searches for similar content in RAG system
	SearchSimilar(ctx context.Context, content string, limit int) ([]*RAGResult, error)
	
	// UpdateIndex updates RAG index with new content
	UpdateIndex(ctx context.Context, updates []*RAGUpdate) error
	
	// GetRAGStats returns RAG system statistics
	GetRAGStats(ctx context.Context) (*RAGStatistics, error)
}

// Supporting types for intelligence operations

// ProjectGoal represents a high-level project objective
type ProjectGoal struct {
	ID          string   `json:"id"`          // Unique identifier
	Name        string   `json:"name"`        // Goal name
	Description string   `json:"description"` // Detailed description
	Keywords    []string `json:"keywords"`    // Associated keywords
	Priority    int      `json:"priority"`    // Priority level (1=highest)
	Phase       string   `json:"phase"`       // Project phase
	Metrics     []string `json:"metrics"`     // Success metrics
	Owner       string   `json:"owner"`       // Goal owner
	Deadline    *time.Time `json:"deadline,omitempty"` // Target deadline
}

// RoleProfile defines context requirements for different roles
type RoleProfile struct {
	Role            string                          `json:"role"`             // Role identifier
	AccessLevel     slurpContext.RoleAccessLevel   `json:"access_level"`     // Required access level
	RelevantTags    []string                       `json:"relevant_tags"`    // Relevant context tags
	ContextScope    []string                       `json:"context_scope"`    // Scope of interest
	InsightTypes    []string                       `json:"insight_types"`    // Types of insights needed
	QualityThreshold float64                       `json:"quality_threshold"` // Minimum quality threshold
	Preferences     map[string]interface{}         `json:"preferences"`      // Role-specific preferences
}

// EngineConfig represents configuration for the intelligence engine
type EngineConfig struct {
	// Analysis settings
	MaxConcurrentAnalysis int           `json:"max_concurrent_analysis"` // Maximum concurrent analyses
	AnalysisTimeout       time.Duration `json:"analysis_timeout"`        // Analysis timeout
	MaxFileSize           int64         `json:"max_file_size"`           // Maximum file size to analyze
	
	// RAG integration settings
	RAGEndpoint           string        `json:"rag_endpoint"`            // RAG system endpoint
	RAGTimeout            time.Duration `json:"rag_timeout"`             // RAG query timeout
	RAGEnabled            bool          `json:"rag_enabled"`             // Whether RAG is enabled
	
	// Quality settings
	MinConfidenceThreshold float64      `json:"min_confidence_threshold"` // Minimum confidence for results
	RequireValidation     bool          `json:"require_validation"`       // Whether validation is required
	
	// Performance settings
	CacheEnabled          bool          `json:"cache_enabled"`           // Whether caching is enabled
	CacheTTL              time.Duration `json:"cache_ttl"`               // Cache TTL
	
	// Role profiles
	RoleProfiles          map[string]*RoleProfile `json:"role_profiles"` // Role-specific profiles
	
	// Project goals
	ProjectGoals          []*ProjectGoal `json:"project_goals"` // Active project goals
}

// EngineStatistics represents performance statistics for the engine
type EngineStatistics struct {
	TotalAnalyses         int64         `json:"total_analyses"`          // Total analyses performed
	SuccessfulAnalyses    int64         `json:"successful_analyses"`     // Successful analyses
	FailedAnalyses        int64         `json:"failed_analyses"`         // Failed analyses
	AverageAnalysisTime   time.Duration `json:"average_analysis_time"`   // Average analysis time
	CacheHitRate          float64       `json:"cache_hit_rate"`          // Cache hit rate
	RAGQueriesPerformed   int64         `json:"rag_queries_performed"`   // RAG queries made
	AverageConfidence     float64       `json:"average_confidence"`      // Average confidence score
	FilesAnalyzed         int64         `json:"files_analyzed"`          // Total files analyzed
	DirectoriesAnalyzed   int64         `json:"directories_analyzed"`    // Total directories analyzed
	PatternsDetected      int64         `json:"patterns_detected"`       // Patterns detected
	LastResetAt           time.Time     `json:"last_reset_at"`           // When stats were last reset
}

// FileAnalysis represents the result of file analysis
type FileAnalysis struct {
	FilePath      string            `json:"file_path"`       // Path to analyzed file
	Language      string            `json:"language"`        // Detected language
	LanguageConf  float64           `json:"language_conf"`   // Language detection confidence
	FileType      string            `json:"file_type"`       // File type classification
	Size          int64             `json:"size"`            // File size in bytes
	LineCount     int               `json:"line_count"`      // Number of lines
	Complexity    float64           `json:"complexity"`      // Code complexity score
	Dependencies  []string          `json:"dependencies"`    // Identified dependencies
	Exports       []string          `json:"exports"`         // Exported symbols/functions
	Imports       []string          `json:"imports"`         // Import statements
	Functions     []string          `json:"functions"`       // Function/method names
	Classes       []string          `json:"classes"`         // Class names
	Variables     []string          `json:"variables"`       // Variable names
	Comments      []string          `json:"comments"`        // Extracted comments
	TODOs         []string          `json:"todos"`           // TODO comments
	Metadata      map[string]interface{} `json:"metadata"`   // Additional metadata
	AnalyzedAt    time.Time         `json:"analyzed_at"`     // When analysis was performed
}

// DefaultIntelligenceEngine provides a complete implementation of the IntelligenceEngine interface
type DefaultIntelligenceEngine struct {
	mu                sync.RWMutex
	config            *EngineConfig
	fileAnalyzer      FileAnalyzer
	directoryAnalyzer DirectoryAnalyzer
	patternDetector   PatternDetector
	ragIntegration    RAGIntegration
	stats             *EngineStatistics
	cache             *sync.Map // Simple cache for analysis results
	projectGoals      []*ProjectGoal
	roleProfiles      map[string]*RoleProfile
}

// CacheEntry represents a cached analysis result
type CacheEntry struct {
	ContextNode *slurpContext.ContextNode
	CreatedAt   time.Time
	ExpiresAt   time.Time
}

// NewDefaultIntelligenceEngine creates a new intelligence engine with default implementations
func NewDefaultIntelligenceEngine(config *EngineConfig) (*DefaultIntelligenceEngine, error) {
	if config == nil {
		config = DefaultEngineConfig()
	}

	// Initialize file analyzer
	fileAnalyzer := NewDefaultFileAnalyzer(config)

	// Initialize directory analyzer
	dirAnalyzer := NewDefaultDirectoryAnalyzer(config)

	// Initialize pattern detector
	patternDetector := NewDefaultPatternDetector(config)

	// Initialize RAG integration (if enabled)
	var ragIntegration RAGIntegration
	if config.RAGEnabled {
		ragIntegration = NewDefaultRAGIntegration(config)
	} else {
		ragIntegration = NewNoOpRAGIntegration()
	}

	engine := &DefaultIntelligenceEngine{
		config:            config,
		fileAnalyzer:      fileAnalyzer,
		directoryAnalyzer: dirAnalyzer,
		patternDetector:   patternDetector,
		ragIntegration:    ragIntegration,
		stats:             &EngineStatistics{
			LastResetAt: time.Now(),
		},
		cache:         &sync.Map{},
		projectGoals:  config.ProjectGoals,
		roleProfiles:  config.RoleProfiles,
	}

	return engine, nil
}