using System.Net;
using System.Net.Http.Json;
using System.Linq;

namespace Baya.Test.Api;

public class AdminVerificationApiTests(BayaApiFactory factory) : IClassFixture<BayaApiFactory>
{
    private static readonly string[] SeededCodes =
    [
        "identity_kyc", "shahkar_match", "moh_competency_license",
        "ino_membership", "criminal_record", "bank_account_verification"
    ];

    [Fact]
    public async Task ListStepTypes_AsAdmin_ContainsTheSixSeededCodes()
    {
        var client = factory.CreateClient();
        await AdminTestClient.AuthenticateAsync(factory, client, "09124200001");

        var response = await client.GetAsync("/api/v1/admin_verification_step_types");
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);

        var codes = (await AuthTestClient.ReadDataAsync(response)).EnumerateArray()
            .Select(s => s.GetProperty("code").GetString())
            .ToHashSet();
        foreach (var code in SeededCodes)
            Assert.Contains(code, codes);
    }

    [Fact]
    public async Task UpsertStepType_NewRow_Persists_ProvesDataDriven()
    {
        var client = factory.CreateClient();
        await AdminTestClient.AuthenticateAsync(factory, client, "09124200002");

        var body = new
        {
            id = (long?)null,
            code = "liability_insurance",
            displayName = "Professional Liability Insurance",
            description = (string?)null,
            isRequired = false,
            isAutomated = false,
            automationProvider = (string?)null,
            sortOrder = 7,
            isActive = true
        };
        var created = await client.PostAsJsonAsync("/api/v1/admin_verification_step_types", body);
        Assert.Equal(HttpStatusCode.OK, created.StatusCode);

        var list = await client.GetAsync("/api/v1/admin_verification_step_types?includeInactive=true");
        var codes = (await AuthTestClient.ReadDataAsync(list)).EnumerateArray()
            .Select(s => s.GetProperty("code").GetString())
            .ToList();
        Assert.Contains("liability_insurance", codes);
    }

    [Fact]
    public async Task UpsertStepType_InvalidCode_Returns400()
    {
        var client = factory.CreateClient();
        await AdminTestClient.AuthenticateAsync(factory, client, "09124200003");

        var body = new
        {
            id = (long?)null,
            code = "Bad Code!",
            displayName = "X",
            description = (string?)null,
            isRequired = false,
            isAutomated = false,
            automationProvider = (string?)null,
            sortOrder = 8,
            isActive = true
        };
        var response = await client.PostAsJsonAsync("/api/v1/admin_verification_step_types", body);
        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
    }

    [Fact]
    public async Task StepTypes_AsNurse_Returns403()
    {
        var client = factory.CreateClient();
        await ProfileTestClient.AuthenticateAsync(factory, client, "09124200004", "nurse");

        var response = await client.GetAsync("/api/v1/admin_verification_step_types");
        Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
    }

    [Fact]
    public async Task StepTypes_Unauthenticated_Returns401()
    {
        var client = factory.CreateClient();
        var response = await client.GetAsync("/api/v1/admin_verification_step_types");
        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
    }
}
