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

namespace Baya.Test.Api;

public class OtpRequestTests(BayaApiFactory factory) : IClassFixture<BayaApiFactory>
{
    [Fact]
    public async Task RequestOtp_ValidPhone_SendsOtpAndOpensResendWindow()
    {
        var client = factory.CreateClient();

        var first = await client.PostAsJsonAsync("/api/v1/auth/request_otp", new { phone = "09120000001" });

        Assert.Equal(HttpStatusCode.OK, first.StatusCode);
        var firstData = await AuthTestClient.ReadDataAsync(first);
        Assert.True(firstData.GetProperty("otpSent").GetBoolean());
        Assert.True(firstData.GetProperty("resendAvailableInSeconds").GetInt32() > 0);

        // Same phone inside the resend window: same non-enumerating shape, otpSent=false.
        var second = await client.PostAsJsonAsync("/api/v1/auth/request_otp", new { phone = "09120000001" });

        Assert.Equal(HttpStatusCode.OK, second.StatusCode);
        var secondData = await AuthTestClient.ReadDataAsync(second);
        Assert.False(secondData.GetProperty("otpSent").GetBoolean());
    }

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

        var response = await client.PostAsJsonAsync("/api/v1/auth/request_otp", new { phone = "12345" });

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