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

namespace CashLineERP.Api.Tests;

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

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

    [Fact]
    public async Task Internal_Workflow_Monitoring_Endpoint_Returns_Operational_Summary_Backlogs_And_Alerts()
    {
        await _client.AuthenticateAsFinanceCheckerAsync();

        var response = await _client.GetAsync("/api/v1/internal/workflow-monitoring?itemLimit=5");

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var payload = await response.Content.ReadFromJsonAsync<Dictionary<string, JsonElement>>();
        Assert.NotNull(payload);
        Assert.True(payload!.ContainsKey("dashboard"));
        Assert.True(payload.ContainsKey("alerts"));
        Assert.True(payload.ContainsKey("pendingApprovals"));
        Assert.True(payload.ContainsKey("openExceptions"));
        Assert.True(payload.ContainsKey("outboxBacklog"));
        Assert.True(payload.ContainsKey("inboxBacklog"));
    }

    [Fact]
    public async Task Supplier_External_Financing_And_Compliance_Endpoints_Return_Server_Scoped_Collections()
    {
        await _client.AuthenticateAsSupplierAsync();

        var requestsResponse = await _client.GetAsync("/api/v1/external/parties/1/financing/requests");
        var murabahaCasesResponse = await _client.GetAsync("/api/v1/external/parties/1/financing/murabaha-cases");
        var disbursementsResponse = await _client.GetAsync("/api/v1/external/parties/1/financing/disbursements");
        var settlementsResponse = await _client.GetAsync("/api/v1/external/parties/1/financing/settlements");
        var complianceDocumentsResponse = await _client.GetAsync("/api/v1/external/parties/1/compliance-documents");

        Assert.Equal(HttpStatusCode.OK, requestsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, murabahaCasesResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, disbursementsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, settlementsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, complianceDocumentsResponse.StatusCode);

        Assert.StartsWith("[", await requestsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await murabahaCasesResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await disbursementsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await settlementsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await complianceDocumentsResponse.Content.ReadAsStringAsync());
    }

    [Fact]
    public async Task Customer_External_Financing_And_Compliance_Endpoints_Return_Server_Scoped_Collections()
    {
        await _client.AuthenticateAsCustomerAsync();

        var requestsResponse = await _client.GetAsync("/api/v1/external/parties/2/financing/requests");
        var murabahaCasesResponse = await _client.GetAsync("/api/v1/external/parties/2/financing/murabaha-cases");
        var disbursementsResponse = await _client.GetAsync("/api/v1/external/parties/2/financing/disbursements");
        var settlementsResponse = await _client.GetAsync("/api/v1/external/parties/2/financing/settlements");
        var complianceDocumentsResponse = await _client.GetAsync("/api/v1/external/parties/2/compliance-documents");

        Assert.Equal(HttpStatusCode.OK, requestsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, murabahaCasesResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, disbursementsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, settlementsResponse.StatusCode);
        Assert.Equal(HttpStatusCode.OK, complianceDocumentsResponse.StatusCode);

        Assert.StartsWith("[", await requestsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await murabahaCasesResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await disbursementsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await settlementsResponse.Content.ReadAsStringAsync());
        Assert.StartsWith("[", await complianceDocumentsResponse.Content.ReadAsStringAsync());
    }
}
