using NatsNet.PortTracker.Data;

namespace NatsNet.PortTracker.Reporting;

public static class ReportGenerator
{
    public static void PrintSummary(Database db)
    {
        Console.WriteLine("=== Porting Status Summary ===\n");

        PrintTableSummary(db, "modules", "Modules");
        PrintTableSummary(db, "features", "Features");
        PrintTableSummary(db, "unit_tests", "Unit Tests");
        PrintLibrarySummary(db);

        // Overall progress
        var totalItems = db.ExecuteScalar<long>("SELECT COUNT(*) FROM modules") +
                         db.ExecuteScalar<long>("SELECT COUNT(*) FROM features") +
                         db.ExecuteScalar<long>("SELECT COUNT(*) FROM unit_tests");
        var doneItems = db.ExecuteScalar<long>("SELECT COUNT(*) FROM modules WHERE status IN ('complete', 'verified', 'n_a')") +
                        db.ExecuteScalar<long>("SELECT COUNT(*) FROM features WHERE status IN ('complete', 'verified', 'n_a')") +
                        db.ExecuteScalar<long>("SELECT COUNT(*) FROM unit_tests WHERE status IN ('complete', 'verified', 'n_a')");
        var pct = totalItems > 0 ? (double)doneItems / totalItems * 100 : 0;
        Console.WriteLine($"\nOverall Progress: {doneItems}/{totalItems} ({pct:F1}%)");
    }

    public static string ExportMarkdown(Database db)
    {
        var sb = new System.Text.StringBuilder();
        sb.AppendLine("# NATS .NET Porting Status Report");
        sb.AppendLine($"\nGenerated: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC\n");

        AppendTableMarkdown(sb, db, "modules", "Modules");
        AppendTableMarkdown(sb, db, "features", "Features");
        AppendTableMarkdown(sb, db, "unit_tests", "Unit Tests");
        AppendLibraryMarkdown(sb, db);

        // Overall
        var totalItems = db.ExecuteScalar<long>("SELECT COUNT(*) FROM modules") +
                         db.ExecuteScalar<long>("SELECT COUNT(*) FROM features") +
                         db.ExecuteScalar<long>("SELECT COUNT(*) FROM unit_tests");
        var doneItems = db.ExecuteScalar<long>("SELECT COUNT(*) FROM modules WHERE status IN ('complete', 'verified', 'n_a')") +
                        db.ExecuteScalar<long>("SELECT COUNT(*) FROM features WHERE status IN ('complete', 'verified', 'n_a')") +
                        db.ExecuteScalar<long>("SELECT COUNT(*) FROM unit_tests WHERE status IN ('complete', 'verified', 'n_a')");
        var pct = totalItems > 0 ? (double)doneItems / totalItems * 100 : 0;
        sb.AppendLine($"\n## Overall Progress\n");
        sb.AppendLine($"**{doneItems}/{totalItems} items complete ({pct:F1}%)**");

        return sb.ToString();
    }

    private static void PrintTableSummary(Database db, string table, string label)
    {
        var rows = db.Query($"SELECT status, COUNT(*) as cnt FROM {table} GROUP BY status ORDER BY status");
        var total = rows.Sum(r => Convert.ToInt64(r["cnt"]));
        Console.WriteLine($"{label} ({total} total):");
        foreach (var row in rows)
            Console.WriteLine($"  {row["status"],-15} {row["cnt"],5}");
        Console.WriteLine();
    }

    private static void PrintLibrarySummary(Database db)
    {
        var rows = db.Query("SELECT status, COUNT(*) as cnt FROM library_mappings GROUP BY status ORDER BY status");
        var total = rows.Sum(r => Convert.ToInt64(r["cnt"]));
        Console.WriteLine($"Library Mappings ({total} total):");
        foreach (var row in rows)
            Console.WriteLine($"  {row["status"],-15} {row["cnt"],5}");
        Console.WriteLine();
    }

    private static void AppendTableMarkdown(System.Text.StringBuilder sb, Database db, string table, string label)
    {
        var rows = db.Query($"SELECT status, COUNT(*) as cnt FROM {table} GROUP BY status ORDER BY status");
        var total = rows.Sum(r => Convert.ToInt64(r["cnt"]));
        sb.AppendLine($"## {label} ({total} total)\n");
        sb.AppendLine("| Status | Count |");
        sb.AppendLine("|--------|-------|");
        foreach (var row in rows)
            sb.AppendLine($"| {row["status"]} | {row["cnt"]} |");
        sb.AppendLine();
    }

    private static void AppendLibraryMarkdown(System.Text.StringBuilder sb, Database db)
    {
        var rows = db.Query("SELECT status, COUNT(*) as cnt FROM library_mappings GROUP BY status ORDER BY status");
        var total = rows.Sum(r => Convert.ToInt64(r["cnt"]));
        sb.AppendLine($"## Library Mappings ({total} total)\n");
        sb.AppendLine("| Status | Count |");
        sb.AppendLine("|--------|-------|");
        foreach (var row in rows)
            sb.AppendLine($"| {row["status"]} | {row["cnt"]} |");
        sb.AppendLine();
    }
}
