using System.Net;
using System.Net.Http.Json;
using CashLineERP.Contracts.Finance;

namespace CashLineERP.Api.Tests;

public sealed class ForeignExchangeOverrideQaTests : IClassFixture<ApiTestFactory>
{
    private readonly HttpClient _client;

    public ForeignExchangeOverrideQaTests(ApiTestFactory factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task FX_Override_Endpoints_Support_Create_Submit_Approve_And_Reads()
    {
        await _client.AuthenticateAsFinanceMakerAsync();

        var createResponse = await _client.PostAsJsonAsync("/api/v1/finance/fx-overrides", new CreateForeignExchangeOverrideRequest(
            EffectiveDate: new DateOnly(2026, 4, 24),
            BaseCurrencyCode: "EGP",
            CurrencyCode: "USD",
            OverrideRateToBase: 47.9m,
            ReasonCode: "TREASURY_MANUAL",
            Notes: "HTTP override"));
        Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode);
        var createdOverride = await createResponse.Content.ReadFromJsonAsync<ForeignExchangeOverrideResponse>();
        Assert.NotNull(createdOverride);
        Assert.Equal("DRAFT", createdOverride!.StatusCode);

        var submitResponse = await _client.PostAsync($"/api/v1/finance/fx-overrides/{createdOverride.ForeignExchangeOverrideId}/submit", null);
        Assert.Equal(HttpStatusCode.OK, submitResponse.StatusCode);
        var submittedOverride = await submitResponse.Content.ReadFromJsonAsync<ForeignExchangeOverrideResponse>();
        Assert.NotNull(submittedOverride);
        Assert.Equal("SUBMITTED", submittedOverride!.StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();

        var approveResponse = await _client.PostAsync($"/api/v1/finance/fx-overrides/{createdOverride.ForeignExchangeOverrideId}/approve", null);
        Assert.Equal(HttpStatusCode.OK, approveResponse.StatusCode);
        var approvedOverride = await approveResponse.Content.ReadFromJsonAsync<ForeignExchangeOverrideResponse>();
        Assert.NotNull(approvedOverride);
        Assert.Equal("APPROVED", approvedOverride!.StatusCode);

        var detailResponse = await _client.GetAsync($"/api/v1/finance/fx-overrides/{createdOverride.ForeignExchangeOverrideId}");
        Assert.Equal(HttpStatusCode.OK, detailResponse.StatusCode);
        var overrideDetail = await detailResponse.Content.ReadFromJsonAsync<ForeignExchangeOverrideResponse>();
        Assert.NotNull(overrideDetail);
        Assert.Equal(47.9m, overrideDetail!.OverrideRateToBase);

        var listResponse = await _client.GetAsync("/api/v1/finance/fx-overrides?currencyCode=USD&baseCurrencyCode=EGP&statusCode=APPROVED");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var overrideList = await listResponse.Content.ReadFromJsonAsync<ForeignExchangeOverrideResponse[]>();
        Assert.NotNull(overrideList);
        var listedOverride = Assert.Single(overrideList!);
        Assert.Equal(createdOverride.ForeignExchangeOverrideId, listedOverride.ForeignExchangeOverrideId);
    }
}
