using System;
using System.IO;
using Xunit;

namespace GmRelay.Bot.Tests.Project;

public class LicenseFileTests
{
    private static string GetRepoRoot()
    {
        var dir = AppContext.BaseDirectory;
        while (!string.IsNullOrEmpty(dir) && !File.Exists(Path.Combine(dir, "Directory.Build.props")))
        {
            dir = Directory.GetParent(dir)?.FullName;
        }
        return dir ?? throw new InvalidOperationException("Could not find repo root");
    }

    [Fact]
    public void LicenseFile_ExistsInRepoRoot()
    {
        var repoRoot = GetRepoRoot();
        var licensePath = Path.Combine(repoRoot, "LICENSE");
        Assert.True(File.Exists(licensePath), "LICENSE file should exist in repo root");
    }

    [Fact]
    public void LicenseFile_ContainsMitLicenseText()
    {
        var repoRoot = GetRepoRoot();
        var licensePath = Path.Combine(repoRoot, "LICENSE");
        Assert.True(File.Exists(licensePath), "LICENSE file should exist");
        var content = File.ReadAllText(licensePath);
        Assert.Contains("MIT License", content);
    }

    [Fact]
    public void Readme_ReferencesLicenseFile()
    {
        var repoRoot = GetRepoRoot();
        var readmePath = Path.Combine(repoRoot, "README.md");
        Assert.True(File.Exists(readmePath), "README.md should exist");
        var content = File.ReadAllText(readmePath);
        Assert.Contains("./LICENSE", content);
    }
}
