using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace Tests.Specs;

[Parallelizable(ParallelScope.Self)]
[FixtureLifeCycle(LifeCycle.SingleInstance)]
public class LinksTests : PageTest
{
    private Website _website = null!;

    [SetUp]
    public void CreateWebsite()
    {
        _website = new Website(Page);
    }

    [Test]
    public async Task VerifyPageLinks()
    {
        _website = new Website(Page);

        await _website.HarassCalculatorPage.GotoAsync();
        var harassLinks = await _website.HarassCalculatorPage.GetLinksAsync();
        foreach (var link in harassLinks) await VerifyLinkAsync(link);

        await _website.DatabasePage.GotoAsync();
        var dbLinks = await _website.DatabasePage.GetLinksAsync();
        foreach (var link in dbLinks) await VerifyLinkAsync(link);

        await _website.DatabaseSinglePage.GotoWithSearchAsync("throne");
        var singleLinks = await _website.DatabaseSinglePage.GetLinksAsync();
        foreach (var link in singleLinks) await VerifyLinkAsync(link);
    }

    private static async Task VerifyLinkAsync(string link)
    {
        if (link.StartsWith("mailto")) return;

        using var client = new HttpClient();
        var response = await client.GetAsync(link);
        Assert.That(response.IsSuccessStatusCode, Is.True, $"Link '{link}' returned {response.StatusCode}");
    }
}