using System.Text;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Core;

// Resolve paths: when run as pre-build from Web project, working dir is Web/
var solutionDir = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), ".."));
var docsDir = Path.GetFullPath(Path.Combine(solutionDir, "..", "fellowship.docs"));
var outputFile = Path.Combine(solutionDir, "Model", "DocsData.cs");

if (!Directory.Exists(docsDir))
{
    Console.Error.WriteLine($"Docs directory not found: {docsDir}");
    return 1;
}

var mdFiles = Directory.EnumerateFiles(docsDir, "*.md", SearchOption.AllDirectories)
    .OrderBy(f => f)
    .ToArray();

Console.WriteLine($"Found {mdFiles.Length} markdown files in {docsDir}");

var sb = new StringBuilder();
sb.AppendLine("// <auto-generated />");
sb.AppendLine("// Generated by the Build project. Do not edit manually.");
sb.AppendLine();
sb.AppendLine("namespace Model;");
sb.AppendLine();
sb.AppendLine("public static class DocsData");
sb.AppendLine("{");
sb.AppendLine("    public static readonly List<DocEntry> All = new()");
sb.AppendLine("    {");

foreach (var file in mdFiles)
{
    var content = File.ReadAllText(file);
    var (yamlBlock, body) = ExtractFrontmatter(content);
    if (yamlBlock == null) continue;

    var fields = ParseYamlFields(yamlBlock);
    var relativePath = Path.GetRelativePath(docsDir, file);
    var fileName = Path.GetFileName(file);
    var typeName = GetValue(fields, "type") ?? "Unknown";

    var csharpType = typeName switch
    {
        "Skill" => "SkillDoc",
        "Debuff" => "DebuffDoc",
        "Buff" => "BuffDoc",
        "Key" or "Mouse" => "KeyDoc",
        "Character" => "CharacterDoc",
        _ => "SkillDoc"
    };

    sb.AppendLine($"        new {csharpType}");
    sb.AppendLine("        {");
    sb.AppendLine($"            FileName = {EscapeString(fileName)},");
    sb.AppendLine($"            FilePath = {EscapeString(relativePath)},");

    if (body != null)
        sb.AppendLine($"            Body = {EscapeString(body.TrimEnd())},");

    var knownProps = typeName switch
    {
        "Skill" => GetSkillProperties(fields),
        "Debuff" => GetDebuffProperties(fields),
        "Buff" => GetBuffProperties(fields),
        "Key" or "Mouse" => GetKeyProperties(fields),
        "Character" => GetCharacterProperties(fields),
        _ => GetSkillProperties(fields)
    };

    foreach (var (propName, propValue) in knownProps)
        sb.AppendLine($"            {propName} = {propValue},");

    sb.AppendLine("        },");
}

sb.AppendLine("    };");
sb.AppendLine("}");

File.WriteAllText(outputFile, sb.ToString());
Console.WriteLine($"Generated {outputFile}");
return 0;

static (string? yaml, string? body) ExtractFrontmatter(string content)
{
    if (!content.StartsWith("---")) return (null, null);

    var endIndex = content.IndexOf("---", 3, StringComparison.Ordinal);
    if (endIndex == -1) return (null, null);

    var yaml = content[3..endIndex].Trim();
    var body = content[(endIndex + 3)..].Trim();
    return (yaml, string.IsNullOrEmpty(body) ? null : body);
}

static Dictionary<string, object?> ParseYamlFields(string yamlBlock)
{
    var result = new Dictionary<string, object?>();
    try
    {
        var yamlStream = new YamlStream();
        yamlStream.Load(new StringReader(yamlBlock));

        if (yamlStream.Documents.Count > 0 && yamlStream.Documents[0].RootNode is YamlMappingNode mapping)
            foreach (var (keyNode, valueNode) in mapping)
            {
                var key = keyNode.ToString();
                result[key] = ConvertYamlNode(valueNode);
            }
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine($"Warning: Failed to parse YAML: {ex.Message}");
    }
    return result;
}

static object? ConvertYamlNode(YamlNode node)
{
    return node switch
    {
        YamlScalarNode scalar => scalar.Value,
        YamlSequenceNode seq => seq.Children.Select(ConvertYamlNode).ToList(),
        YamlMappingNode map => map.ToDictionary(kvp => kvp.Key.ToString(), kvp => ConvertYamlNode(kvp.Value)),
        _ => node.ToString()
    };
}

static string? GetValue(Dictionary<string, object?> fields, string key)
{
    if (fields.TryGetValue(key, out var val) && val is string s && !string.IsNullOrEmpty(s))
        return s;
    return null;
}

static string EscapeString(string? s)
{
    if (s == null) return "null";
    if (s.Contains('\n') || s.Contains('"') || s.Contains('\\'))
    {
        var escaped = s.Replace("\"", "\"\"");
        return $"@\"{escaped}\"";
    }
    return $"\"{s}\"";
}

static string EscapeBool(object? val)
{
    if (val is string s)
    {
        if (bool.TryParse(s, out var b)) return b ? "true" : "false";
        return "null";
    }
    return "null";
}

static string EscapeStringOrNull(object? val)
{
    if (val is string s) return EscapeString(s);
    return "null";
}

static string EscapeList(object? val)
{
    if (val is List<object> list && list.Count > 0)
    {
        var items = list.OfType<string>().Select(EscapeString);
        return $"new() {{ {string.Join(", ", items)} }}";
    }
    return "null";
}

static string EscapeNumericString(object? val)
{
    if (val is string s) return EscapeString(s);
    if (val is int i) return EscapeString(i.ToString());
    if (val is double d) return EscapeString(d.ToString("0.##"));
    return "null";
}

static IEnumerable<(string prop, string value)> GetSkillProperties(Dictionary<string, object?> f)
{
    if (GetValue(f, "character") is { } c) yield return ("Character", EscapeString(c));
    if (f.TryGetValue("cast", out var cast)) yield return ("Cast", EscapeStringOrNull(cast));
    if (f.TryGetValue("description", out var desc)) yield return ("Description", EscapeStringOrNull(desc));
    if (f.TryGetValue("key", out var key)) yield return ("Key", EscapeStringOrNull(key));
    if (f.TryGetValue("range", out var range)) yield return ("Range", EscapeStringOrNull(range));
    if (f.TryGetValue("offGlobalCooldown", out var ogc))
    {
        if (ogc is string s && bool.TryParse(s, out var b))
            yield return ("OffGlobalCooldown", b ? "true" : "false");
        else if (ogc is null)
            yield return ("OffGlobalCooldown", "true");
    }
    if (f.TryGetValue("damage", out var dmg)) yield return ("Damage", EscapeNumericString(dmg));
    if (f.TryGetValue("cooldown", out var cd)) yield return ("Cooldown", EscapeNumericString(cd));
    if (f.TryGetValue("mana", out var mana)) yield return ("Mana", EscapeNumericString(mana));
    if (f.TryGetValue("damageType", out var dt)) yield return ("DamageType", EscapeStringOrNull(dt));
    if (f.TryGetValue("heal", out var heal)) yield return ("Heal", EscapeNumericString(heal));
    if (f.TryGetValue("shield", out var shield)) yield return ("Shield", EscapeNumericString(shield));
    if (f.TryGetValue("order", out var order)) yield return ("Order", EscapeNumericString(order));
    if (f.TryGetValue("priority", out var priority)) yield return ("Priority", EscapeNumericString(priority));
    if (f.TryGetValue("completed", out var completed)) yield return ("Completed", EscapeBool(completed));
    if (f.TryGetValue("gdc", out var gdc)) yield return ("Gdc", EscapeNumericString(gdc));
    if (f.TryGetValue("tags", out var tags)) yield return ("Tags", EscapeList(tags));
    if (f.TryGetValue("related", out var related) || f.TryGetValue("releated", out related))
        yield return ("Related", EscapeList(related));
    if (f.TryGetValue("swiftReprievalChance", out var src)) yield return ("SwiftReprievalChance", EscapeNumericString(src));
    if (f.TryGetValue("areaDamagePercentage", out var adp)) yield return ("AreaDamagePercentage", EscapeNumericString(adp));
    if (f.TryGetValue("generatesSpirit", out var gs)) yield return ("GeneratesSpirit", EscapeNumericString(gs));
    if (f.TryGetValue("duration", out var dur)) yield return ("Duration", EscapeNumericString(dur));
    if (f.TryGetValue("damageReduction", out var dr)) yield return ("DamageReduction", EscapeNumericString(dr));
    if (f.TryGetValue("damageTickTime", out var dtt)) yield return ("DamageTickTime", EscapeNumericString(dtt));
    if (f.TryGetValue("isToggle", out var isTog)) yield return ("IsToggle", EscapeBool(isTog));
    if (f.TryGetValue("manaUpkeepTick", out var mut)) yield return ("ManaUpkeepTick", EscapeNumericString(mut));
    if (f.TryGetValue("parryChance", out var pc)) yield return ("ParryChance", EscapeNumericString(pc));
    if (f.TryGetValue("damageRedirection", out var dredirect)) yield return ("DamageRedirection", EscapeNumericString(dredirect));
    if (f.TryGetValue("spiritCost", out var sc)) yield return ("SpiritCost", EscapeNumericString(sc));
    if (f.TryGetValue("secondEffectDuration", out var sed)) yield return ("SecondEffectDuration", EscapeNumericString(sed));
    if (f.TryGetValue("secondEffectDamageReduction", out var sedr)) yield return ("SecondEffectDamageReduction", EscapeNumericString(sedr));
    if (f.TryGetValue("healingDuration", out var hd)) yield return ("HealingDuration", EscapeNumericString(hd));
    if (f.TryGetValue("healingTickTime", out var htt)) yield return ("HealingTickTime", EscapeNumericString(htt));
    if (f.TryGetValue("costSwiftReprieval", out var csr)) yield return ("CostSwiftReprieval", EscapeNumericString(csr));
    if (f.TryGetValue("gdcDuration", out var gd)) yield return ("GdcDuration", EscapeNumericString(gd));
    if (f.TryGetValue("effect", out var effect)) yield return ("Effect", EscapeStringOrNull(effect));
    if (f.TryGetValue("raw", out var raw)) yield return ("Raw", EscapeStringOrNull(raw));
}

static IEnumerable<(string prop, string value)> GetDebuffProperties(Dictionary<string, object?> f)
{
    if (GetValue(f, "character") is { } c) yield return ("Character", EscapeString(c));
    if (f.TryGetValue("maxStacks", out var ms)) yield return ("MaxStacks", EscapeNumericString(ms));
    if (f.TryGetValue("duration", out var dur)) yield return ("Duration", EscapeNumericString(dur));
    if (f.TryGetValue("description", out var desc)) yield return ("Description", EscapeStringOrNull(desc));
    if (f.TryGetValue("parryChanceBonus", out var pcb)) yield return ("ParryChanceBonus", EscapeNumericString(pcb));
    if (f.TryGetValue("manaRestoreBase", out var mrb)) yield return ("ManaRestoreBase", EscapeNumericString(mrb));
    if (f.TryGetValue("manaRestorePerStack", out var mrps)) yield return ("ManaRestorePerStack", EscapeNumericString(mrps));
}

static IEnumerable<(string prop, string value)> GetBuffProperties(Dictionary<string, object?> f)
{
    if (GetValue(f, "character") is { } c) yield return ("Character", EscapeString(c));
    if (f.TryGetValue("maxStacks", out var ms)) yield return ("MaxStacks", EscapeNumericString(ms));
    if (f.TryGetValue("description", out var desc)) yield return ("Description", EscapeStringOrNull(desc));
}

static IEnumerable<(string prop, string value)> GetKeyProperties(Dictionary<string, object?> f)
{
    if (f.TryGetValue("action", out var action)) yield return ("Action", EscapeStringOrNull(action));
}

static IEnumerable<(string prop, string value)> GetCharacterProperties(Dictionary<string, object?> f)
{
    if (GetValue(f, "character") is { } c) yield return ("Character", EscapeString(c));
}
