namespace NeonSprawl.Server.Game.Gathering;

/// <summary>Resolves resource-node catalog paths for local dev, tests, and container layouts (NEO-58).</summary>
public static class ResourceNodeCatalogPathResolution
{
    /// <summary>Walks <paramref name="startDirectory"/> and parents for an existing <c>content/resource-nodes</c> directory.</summary>
    public static string? TryDiscoverResourceNodesDirectory(string startDirectory)
    {
        for (var dir = new DirectoryInfo(startDirectory); dir is not null; dir = dir.Parent)
        {
            var candidate = Path.Combine(dir.FullName, "content", "resource-nodes");
            if (Directory.Exists(candidate))
                return Path.GetFullPath(candidate);
        }

        return null;
    }

    /// <summary>
    /// Resolves the resource-nodes catalog directory.
    /// Empty <paramref name="configuredResourceNodesDirectory"/> triggers discovery from <see cref="AppContext.BaseDirectory"/>.
    /// </summary>
    public static string ResolveResourceNodesDirectory(string? configuredResourceNodesDirectory, string contentRootPath)
    {
        if (string.IsNullOrWhiteSpace(configuredResourceNodesDirectory))
        {
            var discovered = TryDiscoverResourceNodesDirectory(AppContext.BaseDirectory);
            if (discovered is not null)
                return discovered;

            throw new InvalidOperationException(
                "Content:ResourceNodesDirectory is not set and auto-discovery failed (no ancestor of AppContext.BaseDirectory contains 'content/resource-nodes'). " +
                "Set Content:ResourceNodesDirectory in configuration or environment (e.g. Content__ResourceNodesDirectory).");
        }

        var trimmed = configuredResourceNodesDirectory.Trim();
        if (Path.IsPathRooted(trimmed))
            return Path.GetFullPath(trimmed);

        return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
    }

    /// <summary>Resolves JSON Schema path for a single resource node row (Draft 2020-12).</summary>
    public static string ResolveResourceNodeDefSchemaPath(
        string resourceNodesDirectory,
        string? configuredSchemaPath,
        string contentRootPath)
    {
        if (!string.IsNullOrWhiteSpace(configuredSchemaPath))
        {
            var trimmed = configuredSchemaPath.Trim();
            if (Path.IsPathRooted(trimmed))
                return Path.GetFullPath(trimmed);

            return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
        }

        return Path.GetFullPath(Path.Combine(resourceNodesDirectory, "..", "schemas", "resource-node-def.schema.json"));
    }

    /// <summary>Resolves JSON Schema path for a single resource yield row (Draft 2020-12).</summary>
    public static string ResolveResourceYieldRowSchemaPath(
        string resourceNodesDirectory,
        string? configuredSchemaPath,
        string contentRootPath)
    {
        if (!string.IsNullOrWhiteSpace(configuredSchemaPath))
        {
            var trimmed = configuredSchemaPath.Trim();
            if (Path.IsPathRooted(trimmed))
                return Path.GetFullPath(trimmed);

            return Path.GetFullPath(Path.Combine(contentRootPath, trimmed));
        }

        return Path.GetFullPath(Path.Combine(resourceNodesDirectory, "..", "schemas", "resource-yield-row.schema.json"));
    }
}
