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

namespace Baya.Test.Api;

public class BookingRequestsApiTests(BayaApiFactory factory) : IClassFixture<BayaApiFactory>
{
    private static object ValidShapeButBadGender() => new
    {
        nurseId = 1,
        variantId = 1,
        patientId = 1,
        customerAddressId = 1,
        requestedDate = "2027-01-01",
        requestedTimeStart = "09:00:00",
        requestedTimeEnd = "13:00:00",
        requiredCaregiverGender = "other",
        customerNotes = "x"
    };

    [Fact]
    public async Task Create_Unauthenticated_Returns401()
    {
        var client = factory.CreateClient();

        var response = await client.PostAsJsonAsync("/api/v1/booking_requests/create", ValidShapeButBadGender());

        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
    }

    [Fact]
    public async Task Create_InvalidGender_Returns400()
    {
        var client = factory.CreateClient();
        await ProfileTestClient.AuthenticateAsync(factory, client, "09131000101", "customer");

        var response = await client.PostAsJsonAsync("/api/v1/booking_requests/create", ValidShapeButBadGender());

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

    [Fact]
    public async Task List_Authenticated_ReturnsEmptyPagedEnvelope()
    {
        var client = factory.CreateClient();
        await ProfileTestClient.AuthenticateAsync(factory, client, "09131000102", "customer");

        var response = await client.GetAsync("/api/v1/booking_requests/list");

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var data = await AuthTestClient.ReadDataAsync(response);
        Assert.Equal(0, data.GetProperty("total").GetInt32());
    }

    [Fact]
    public async Task Expire_Unauthenticated_Returns401()
    {
        var client = factory.CreateClient();

        var response = await client.PostAsync("/api/v1/admin_booking_requests/expire", null);

        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
    }

    [Fact]
    public async Task Expire_AsAdmin_ReturnsZeroCountsOnEmptySet()
    {
        var admin = factory.CreateClient();
        await AdminTestClient.AuthenticateAsync(factory, admin, "09131000103");

        var response = await admin.PostAsync("/api/v1/admin_booking_requests/expire", null);

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var data = await AuthTestClient.ReadDataAsync(response);
        Assert.Equal(0, data.GetProperty("expiredNoResponse").GetInt32());
        Assert.Equal(0, data.GetProperty("paymentDeadlineExpired").GetInt32());
    }
}
