namespace RolemasterDb.ImportTool;

public sealed class RepositoryPaths
{
    private RepositoryPaths(string rootPath)
    {
        RootPath = rootPath;
        ManifestPath = Path.Combine(rootPath, "sources", "critical-import-manifest.json");
        DefaultDatabasePath = Path.Combine(rootPath, "src", "RolemasterDb.App", "rolemaster.db");
        ArtifactsRootPath = Path.Combine(rootPath, "src", "RolemasterDb.App", "import-artifacts", "critical");
    }

    public string RootPath { get; }
    public string ManifestPath { get; }
    public string DefaultDatabasePath { get; }
    public string ArtifactsRootPath { get; }

    public static RepositoryPaths Discover()
    {
        var probe = new DirectoryInfo(Directory.GetCurrentDirectory());

        while (probe is not null)
        {
            if (File.Exists(Path.Combine(probe.FullName, "RolemasterDB.slnx")))
            {
                return new RepositoryPaths(probe.FullName);
            }

            probe = probe.Parent;
        }

        throw new InvalidOperationException("Could not discover the repository root from the current directory.");
    }
}