using NexusInstaller.Models;
using Xunit;

namespace NexusInstaller.Tests;

public sealed class NexusInstallerOnboardingTests
{
    [Fact]
    public void OrganizationCatalog_IsNotEmpty()
    {
        Assert.NotEmpty(OrganizationCatalog.All);
    }

    [Fact]
    public void OrganizationCatalog_EntriesHaveIdAndName()
    {
        foreach (var org in OrganizationCatalog.All)
        {
            Assert.False(string.IsNullOrWhiteSpace(org.Id));
            Assert.False(string.IsNullOrWhiteSpace(org.Name));
            Assert.Equal(org.Name, org.DisplayName);
            Assert.Equal(org.Name, org.ToString());
        }
    }

    [Theory]
    [InlineData(null, "user@test.local", "PC-1", "User", false)]
    [InlineData("ORG-RENNINJA", "", "PC-1", "User", false)]
    [InlineData("ORG-RENNINJA", "user@test.local", "  ", "User", false)]
    [InlineData("ORG-RENNINJA", "user@test.local", "Jacob-PC", "User", true)]
    public void IdentityStep_ValidationRules(
        string? orgId,
        string userEmail,
        string deviceName,
        string roleName,
        bool expectedValid)
    {
        OrganizationOption? org = orgId is null
            ? null
            : OrganizationCatalog.All.First(o => o.Id == orgId);

        RoleOption? role = RoleCatalog.All.FirstOrDefault(r => r.Name == roleName);

        var isValid = org is not null
            && role is not null
            && !string.IsNullOrWhiteSpace(userEmail)
            && !string.IsNullOrWhiteSpace(deviceName);

        Assert.Equal(expectedValid, isValid);
    }

    [Fact]
    public void RoleCatalog_ContainsUserRole()
    {
        Assert.Contains(RoleCatalog.All, r => r.Name == "User");
    }

    [Fact]
    public void RoleCatalog_EntriesDisplayFriendlyNames()
    {
        foreach (var role in RoleCatalog.All)
        {
            Assert.Equal(role.Name, role.DisplayName);
            Assert.Equal(role.Name, role.ToString());
        }
    }

    [Fact]
    public void InstallerBuildInfo_UsesNextBuildAfterLatestManifest()
    {
        Assert.Equal(100, InstallerBuildInfo.Build);
        Assert.Equal("0.1.0-storefix1", InstallerBuildInfo.Version);
    }

    [Fact]
    public void RoleCatalog_AutoSelectSingleRole()
    {
        var single = new[] { RoleCatalog.All[0] };
        var selected = single.Length == 1 ? single[0] : single.FirstOrDefault();
        Assert.NotNull(selected);
    }
}
