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

namespace Baya.Test.Api;

public class NurseVerificationApiTests(BayaApiFactory factory) : IClassFixture<BayaApiFactory>
{
    private static object NurseProfileBody => new
    {
        bio = "Experienced home nurse",
        yearsOfExperience = 5,
        educationLevel = "BSc",
        educationField = "Nursing",
        specializationsJson = "[]"
    };

    [Fact]
    public async Task Submit_SeedsChecklist_ThenIdentityKycPasses()
    {
        var client = factory.CreateClient();
        await ProfileTestClient.AuthenticateAsync(factory, client, "09124100001", "nurse");
        await client.PostAsJsonAsync("/api/v1/nurse_profiles/upsert", NurseProfileBody);

        var submit = await client.PostAsJsonAsync("/api/v1/nurse_verification/submit", new { });
        Assert.Equal(HttpStatusCode.OK, submit.StatusCode);
        var status = await AuthTestClient.ReadDataAsync(submit);
        Assert.Equal("pending", status.GetProperty("status").GetString());
        Assert.False(status.GetProperty("isBookable").GetBoolean());
        var steps = status.GetProperty("steps").EnumerateArray().ToList();
        Assert.Equal(6, steps.Count);
        // is_automated is snapshotted onto each step.
        Assert.True(steps.Single(s => s.GetProperty("code").GetString() == "identity_kyc").GetProperty("isAutomated").GetBoolean());
        Assert.False(steps.Single(s => s.GetProperty("code").GetString() == "moh_competency_license").GetProperty("isAutomated").GetBoolean());

        var run = await client.PostAsJsonAsync("/api/v1/nurse_verification/steps/identity_kyc/run",
            new { nationalId = "0012345678", livenessPayload = (string?)null });
        Assert.Equal(HttpStatusCode.OK, run.StatusCode);
        Assert.Equal("passed", (await AuthTestClient.ReadDataAsync(run)).GetProperty("stepStatus").GetString());

        var get = await client.GetAsync("/api/v1/nurse_verification");
        var getData = await AuthTestClient.ReadDataAsync(get);
        var identityStep = getData.GetProperty("steps").EnumerateArray()
            .Single(s => s.GetProperty("code").GetString() == "identity_kyc");
        Assert.Equal("passed", identityStep.GetProperty("status").GetString());
    }

    [Fact]
    public async Task RunIdentityKyc_InvalidNationalId_Returns400()
    {
        var client = factory.CreateClient();
        await ProfileTestClient.AuthenticateAsync(factory, client, "09124100002", "nurse");
        await client.PostAsJsonAsync("/api/v1/nurse_profiles/upsert", NurseProfileBody);
        await client.PostAsJsonAsync("/api/v1/nurse_verification/submit", new { });

        var run = await client.PostAsJsonAsync("/api/v1/nurse_verification/steps/identity_kyc/run",
            new { nationalId = "123", livenessPayload = (string?)null });

        Assert.Equal(HttpStatusCode.BadRequest, run.StatusCode);
    }

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