using MarketAlly.AIPlugin.Analysis.Plugins; using MarketAlly.AIPlugin.Refactoring.Plugins; using RefactorIQ.Core.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace MarketAlly.AIPlugin.Learning { public class LearningSession { public string SolutionPath { get; set; } public string DatabasePath { get; set; } public string ModularMapPath { get; set; } public int MaxIterations { get; set; } public bool AutoApply { get; set; } public bool CreateBackup { get; set; } public DateTime StartTime { get; set; } } public class LearningResult { public Guid SessionId { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public TimeSpan TotalDuration { get; set; } public string BackupPath { get; set; } public CompilationResult BaselineCompilation { get; set; } public List Iterations { get; set; } = new List(); public int TotalSuggestions => Iterations.Sum(i => i.Suggestions.Count); public int TotalAppliedChanges => Iterations.Sum(i => i.AppliedChanges.Count); public int SuccessfulChanges => Iterations.Sum(i => i.AppliedChanges.Count(c => c.Success)); public double SuccessRate => TotalAppliedChanges > 0 ? (double)SuccessfulChanges / TotalAppliedChanges : 0; } public class LearningIteration { public int IterationNumber { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public TimeSpan Duration { get; set; } public string TargetFile { get; set; } public int IssuesFound { get; set; } public int FixesApplied { get; set; } public bool Success { get; set; } public bool CriticalError { get; set; } public string ErrorMessage { get; set; } public string Summary { get; set; } public bool ShouldStopSession { get; set; } public CodeAnalysisSnapshot CodeAnalysis { get; set; } public CompilationResult CompilationResult { get; set; } public List FailedAttempts { get; set; } = new(); public List Suggestions { get; set; } = new(); public CompilationResult PostChangeCompilation { get; set; } public List AppliedChanges { get; set; } = new(); } public class CodeAnalysisSnapshot { public DateTime Timestamp { get; set; } public List Issues { get; set; } public CodeMetrics Metrics { get; set; } public List ModularInsights { get; set; } } public class CodeIssue { public string Type { get; set; } public string Description { get; set; } public string FilePath { get; set; } public int LineNumber { get; set; } public string Severity { get; set; } public string Suggestion { get; set; } } public class CodeMetrics { public int TotalFiles { get; set; } public int TotalLines { get; set; } public double AverageComplexity { get; set; } public int TotalIssues { get; set; } } public class ModularInsight { public string Type { get; set; } public string Module { get; set; } public string Description { get; set; } public string Severity { get; set; } } public class AISuggestion { public Guid Id { get; set; } public SuggestionType Type { get; set; } public string Description { get; set; } public string TargetFile { get; set; } public int TargetLine { get; set; } public double Confidence { get; set; } public double ExpectedImprovement { get; set; } public double RiskLevel { get; set; } public DateTime GeneratedAt { get; set; } public string ProposedChange { get; set; } public Dictionary Metadata { get; set; } = new Dictionary(); } public class AppliedChange { public Guid SuggestionId { get; set; } public string FilePath { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public bool Success { get; set; } public string ErrorMessage { get; set; } public string OriginalContent { get; set; } public string ModifiedContent { get; set; } } public enum SuggestionType { ExtractMethod, RenameVariable, AddDocumentation, SimplifyExpression, RemoveDeadCode, ReduceCoupling, ExtractInterface, AddUnitTest, RefactorConditional, Other } // Modular analysis integration public class ModularAnalyzer { public async Task LoadModularMapAsync(string mapPath) { var json = await File.ReadAllTextAsync(mapPath); return JsonSerializer.Deserialize(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } } public class LearningRecord { public Guid SuggestionId { get; set; } public SuggestionType SuggestionType { get; set; } public double Confidence { get; set; } public double ExpectedImprovement { get; set; } public double ActualImprovement { get; set; } public bool Success { get; set; } public CompilationStatus CompilationStatus { get; set; } public int ErrorCountBefore { get; set; } public int ErrorCountAfter { get; set; } public DateTime Timestamp { get; set; } } public class SuccessPattern { public string PatternName { get; set; } public double SuccessRate { get; set; } public double AverageImprovement { get; set; } public int UsageCount { get; set; } public string IssueType { get; set; } } public class FailurePattern { public string IssueType { get; set; } public double FailureRate { get; set; } public int FailureCount { get; set; } } // ModularMap data structures (simplified) public class ModularMapData { public CouplingMetrics CouplingMetrics { get; set; } public Statistics Statistics { get; set; } } public class CouplingMetrics { public List HighlyCoupledModules { get; set; } public List LooselyCoupledModules { get; set; } } public class Statistics { public int TotalModules { get; set; } public int TotalDependencies { get; set; } } // Learning-specific configuration that wraps RefactorIQ options public class LearningRefactorIQConfig { public string ConnectionString { get; set; } public string OpenAIApiKey { get; set; } public bool EnableIncrementalIndexing { get; set; } = true; public bool EnableXamlAnalysis { get; set; } = true; public int MaxConcurrentEmbeddings { get; set; } = 5; public bool EnableVectorSearch { get; set; } = true; public double MinSimilarityScore { get; set; } = 0.7; public int DefaultSearchResults { get; set; } = 10; } // Learning-specific search result wrapper public class LearningVectorSearchResult { public string FilePath { get; set; } public string SymbolName { get; set; } public string SymbolType { get; set; } public string Content { get; set; } public double SimilarityScore { get; set; } public int LineNumber { get; set; } public string ProjectName { get; set; } public Dictionary Metadata { get; set; } = new(); // Factory method to create from RefactorIQ VectorSearchResult public static LearningVectorSearchResult FromRefactorIQResult(RefactorIQ.Core.Models.VectorSearchResult refactorIQResult) { return new LearningVectorSearchResult { FilePath = refactorIQResult.FilePath ?? string.Empty, SymbolName = refactorIQResult.SymbolName ?? string.Empty, SymbolType = refactorIQResult.SymbolType ?? string.Empty, Content = string.Empty, // VectorSearchResult doesn't have Content property SimilarityScore = refactorIQResult.Score, LineNumber = refactorIQResult.LineStart, ProjectName = string.Empty, // VectorSearchResult doesn't have ProjectName property Metadata = new Dictionary { ["Summary"] = refactorIQResult.Summary ?? string.Empty, ["Embedding"] = refactorIQResult.Embedding?.Length ?? 0 } }; } } // Learning-specific embedding statistics public class LearningEmbeddingStats { public int TotalEmbeddings { get; set; } public int PendingEmbeddings { get; set; } public DateTime LastUpdated { get; set; } public Dictionary ProjectStats { get; set; } = new(); // Factory method to create from RefactorIQ stats public static LearningEmbeddingStats FromRefactorIQStats(Dictionary refactorIQStats) { return new LearningEmbeddingStats { TotalEmbeddings = refactorIQStats.GetValueOrDefault("TotalEmbeddings", 0), PendingEmbeddings = refactorIQStats.GetValueOrDefault("PendingEmbeddings", 0), LastUpdated = DateTime.UtcNow, ProjectStats = refactorIQStats }; } } public class ComprehensiveLearningSession { public Guid SessionId { get; set; } public string SolutionPath { get; set; } public string ReportsDirectory { get; set; } public int MaxIterations { get; set; } public int MaxAttemptsPerFile { get; set; } public int SessionTimeoutMinutes { get; set; } public bool VerboseReporting { get; set; } public string LearningMode { get; set; } public bool SkipWarningsAnalysis { get; set; } public string ConfigPath { get; set; } public bool EnableAIEmbeddings { get; set; } public bool EnableSemanticSearch { get; set; } public string OpenAIApiKey { get; set; } public DateTime StartTime { get; set; } } public class ComprehensiveLearningResult { public Guid SessionId { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public TimeSpan TotalDuration { get; set; } public string ProjectName { get; set; } public bool Success { get; set; } public bool CriticalError { get; set; } public string ErrorMessage { get; set; } // Git Information public GitBranchInfo GitInfo { get; set; } // Analysis Results public CompilationResult BaselineCompilation { get; set; } public CompilationResult FinalCompilation { get; set; } public object InitialModularAnalysis { get; set; } public object FinalModularAnalysis { get; set; } public RefactorIQResult InitialRefactorIQAnalysis { get; set; } public RefactorIQResult FinalRefactorIQAnalysis { get; set; } public object InitialWarningsAnalysis { get; set; } public object FinalWarningsAnalysis { get; set; } // Learning Results public List Iterations { get; set; } = new(); public List FailedAttempts { get; set; } = new(); // AI Features Results public LearningEmbeddingStats EmbeddingStats { get; set; } public List SemanticSearchResults { get; set; } = new(); public bool AIFeaturesEnabled { get; set; } // Summary Statistics public int TotalFilesProcessed => Iterations.Count; public int SuccessfulIterations => Iterations.Count(i => i.Success); public int TotalFixesApplied => Iterations.Sum(i => i.FixesApplied); public double SuccessRate => TotalFilesProcessed > 0 ? (double)SuccessfulIterations / TotalFilesProcessed : 0; } public class FailedAttempt { public string FilePath { get; set; } public int AttemptNumber { get; set; } public string FixApproach { get; set; } public string Error { get; set; } public DateTime Timestamp { get; set; } public string HumanReviewNotes { get; set; } = ""; } public class FixResult { public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public bool Success { get; set; } public string Error { get; set; } public string Approach { get; set; } public int FixesApplied { get; set; } public object Details { get; set; } } }