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

namespace CashLineERP.Api.Tests;

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

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

    [Fact]
    public async Task Islamic_Finance_Partner_Lender_Endpoints_Support_Create_Submit_Approve_Activate_And_Reads()
    {
        await _client.AuthenticateAsInternalAsync();

        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/partners", new CreateIslamicFinancePartnerLenderRequest
        {
            PartnerLenderPartyId = 1,
            DefaultCurrencyCode = "EGP",
            SupportsMurabaha = true,
            SupportsReceivablesFinance = false,
            SupportsTawarruq = false
        });

        Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode);
        var created = await createResponse.Content.ReadFromJsonAsync<IslamicFinancePartnerLenderResponse>();
        Assert.NotNull(created);
        Assert.Equal("DRAFT", created!.StatusCode);

        var submitResponse = await _client.PostAsync($"/api/v1/islamic-finance/partners/{created.Id}/submit", null);
        Assert.Equal(HttpStatusCode.OK, submitResponse.StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();

        var approveResponse = await _client.PostAsync($"/api/v1/islamic-finance/partners/{created.Id}/approve", null);
        Assert.Equal(HttpStatusCode.OK, approveResponse.StatusCode);

        var activateResponse = await _client.PostAsync($"/api/v1/islamic-finance/partners/{created.Id}/activate", null);
        Assert.Equal(HttpStatusCode.OK, activateResponse.StatusCode);
        var activated = await activateResponse.Content.ReadFromJsonAsync<IslamicFinancePartnerLenderResponse>();
        Assert.NotNull(activated);
        Assert.True(activated!.IsActive);
        Assert.Equal("ACTIVE", activated.StatusCode);

        var detailResponse = await _client.GetAsync($"/api/v1/islamic-finance/partners/{created.Id}");
        Assert.Equal(HttpStatusCode.OK, detailResponse.StatusCode);

        var listResponse = await _client.GetAsync("/api/v1/islamic-finance/partners?activeOnly=true");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var list = await listResponse.Content.ReadFromJsonAsync<IslamicFinancePartnerLenderResponse[]>();
        Assert.NotNull(list);
        Assert.Single(list!);
    }

    [Fact]
    public async Task Islamic_Finance_Product_Profile_Endpoints_Support_Create_Submit_Approve_Activate_And_Reads()
    {
        await _client.AuthenticateAsInternalAsync();

        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/product-profiles", new CreateIslamicFinanceProductProfileRequest
        {
            ProductType = "MURABAHA",
            ProductCode = "MUR-STD-001",
            ProductName = "Murabaha Standard",
            RequiresShariahReview = true,
            AllowsPartnerFunding = true,
            AllowsCashLineFunding = false
        });

        Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode);
        var created = await createResponse.Content.ReadFromJsonAsync<IslamicFinanceProductProfileResponse>();
        Assert.NotNull(created);
        Assert.Equal("DRAFT", created!.StatusCode);

        var submitResponse = await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}/submit", null);
        Assert.Equal(HttpStatusCode.OK, submitResponse.StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();

        var approveResponse = await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}/approve", null);
        Assert.Equal(HttpStatusCode.OK, approveResponse.StatusCode);

        var activateResponse = await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}/activate", null);
        Assert.Equal(HttpStatusCode.OK, activateResponse.StatusCode);
        var activated = await activateResponse.Content.ReadFromJsonAsync<IslamicFinanceProductProfileResponse>();
        Assert.NotNull(activated);
        Assert.True(activated!.IsActive);
        Assert.Equal("ACTIVE", activated.StatusCode);

        var detailResponse = await _client.GetAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}");
        Assert.Equal(HttpStatusCode.OK, detailResponse.StatusCode);

        var listResponse = await _client.GetAsync("/api/v1/islamic-finance/product-profiles?activeOnly=true&productType=MURABAHA");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var list = await listResponse.Content.ReadFromJsonAsync<IslamicFinanceProductProfileResponse[]>();
        Assert.NotNull(list);
        Assert.Single(list!);
    }
}
