using System.Net;
using System.Net.Http.Json;
using CashLineERP.Application.IslamicFinance.PartnerLenders;
using CashLineERP.Contracts.IslamicFinance;
using CashLineERP.Infrastructure.Persistence;
using CashLineERP.Infrastructure.Persistence.Repositories.IslamicFinance;
using Microsoft.Extensions.DependencyInjection;

namespace CashLineERP.Api.Tests;

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

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

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

        var partner = await CreateActivePartnerAsync();
        var product = await CreateActiveProductAsync();

        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/facilities", new CreateIslamicFinanceFacilityRequest
        {
            PartnerLenderId = partner.Id,
            CustomerPartyId = 1,
            ProductProfileId = product.Id,
            FacilityNumber = $"FAC-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            CurrencyCode = "EGP",
            ApprovedLimitAmount = 250000m,
            UtilizedAmount = 25000m
        });

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

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

        await _client.AuthenticateAsFinanceCheckerAsync();

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

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

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

        var listResponse = await _client.GetAsync($"/api/v1/islamic-finance/facilities?activeOnly=true&partnerLenderId={partner.Id}");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var list = await listResponse.Content.ReadFromJsonAsync<IslamicFinanceFacilityResponse[]>();
        Assert.NotNull(list);
        Assert.Contains(list!, item => item.Id == created.Id);
    }

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

        var product = await CreateActiveProductAsync();
        var requestNumber = $"REQ-{Guid.NewGuid():N}"[..12].ToUpperInvariant();

        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/requests", new CreateIslamicFinanceFinancingRequestRequest
        {
            RequestNumber = requestNumber,
            CustomerPartyId = 1,
            ProductProfileId = product.Id,
            FundingSourceMode = "PARTNER",
            SourceDocumentType = "SALES_ORDER",
            SourceDocumentId = 1001,
            TransactionCurrencyCode = "EGP",
            RequestedAmount = 180000m
        });

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

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

        await _client.AuthenticateAsFinanceCheckerAsync();

        var returnResponse = await _client.PostAsync($"/api/v1/islamic-finance/requests/{created.Id}/return", null);
        Assert.Equal(HttpStatusCode.OK, returnResponse.StatusCode);
        var returned = await returnResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(returned);
        Assert.Equal("RETURNED", returned!.StatusCode);

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

        await _client.AuthenticateAsFinanceCheckerAsync();
        var approveResponse = await _client.PostAsync($"/api/v1/islamic-finance/requests/{created.Id}/approve", null);
        Assert.Equal(HttpStatusCode.OK, approveResponse.StatusCode);
        var approved = await approveResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(approved);
        Assert.Equal("APPROVED", approved!.StatusCode);

        await _client.AuthenticateAsInternalAsync();
        var rejectedCreateResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/requests", new CreateIslamicFinanceFinancingRequestRequest
        {
            RequestNumber = $"REQ-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            CustomerPartyId = 1,
            ProductProfileId = product.Id,
            FundingSourceMode = "PARTNER",
            SourceDocumentType = "SALES_ORDER",
            SourceDocumentId = 1002,
            TransactionCurrencyCode = "EGP",
            RequestedAmount = 95000m
        });

        var rejectedCandidate = await rejectedCreateResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(rejectedCandidate);
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{rejectedCandidate!.Id}/submit", null);

        await _client.AuthenticateAsFinanceCheckerAsync();
        var rejectResponse = await _client.PostAsync($"/api/v1/islamic-finance/requests/{rejectedCandidate.Id}/reject", null);
        Assert.Equal(HttpStatusCode.OK, rejectResponse.StatusCode);
        var rejected = await rejectResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(rejected);
        Assert.Equal("REJECTED", rejected!.StatusCode);

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

        var listResponse = await _client.GetAsync("/api/v1/islamic-finance/requests?statusCode=APPROVED&customerPartyId=1");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var list = await listResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse[]>();
        Assert.NotNull(list);
        Assert.Single(list!);
    }

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

        var product = await CreateActiveProductAsync();
        var request = await CreateSubmittedRequestAsync(product.Id);

        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{request.Id}/approve", null);

        await _client.AuthenticateAsInternalAsync();
        var reviewCreateResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/shariah-reviews", new CreateIslamicFinanceShariahReviewRequest
        {
            ReviewCaseNumber = $"SHR-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            ProductProfileId = product.Id,
            FinancingRequestId = request.Id,
            ReviewReasonCode = "SHARIAH_REVIEW"
        });

        var review = await reviewCreateResponse.Content.ReadFromJsonAsync<IslamicFinanceShariahReviewResponse>();
        Assert.NotNull(review);

        var attachResponse = await _client.PostAsJsonAsync(
            $"/api/v1/islamic-finance/requests/{request.Id}/attach-shariah-review",
            new AttachIslamicFinanceShariahReviewRequest
            {
                ShariahReviewCaseId = review!.Id
            });

        Assert.Equal(HttpStatusCode.OK, attachResponse.StatusCode);
        var attached = await attachResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(attached);
        Assert.Equal(review.Id, attached!.ShariahReviewCaseId);
    }

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

        var product = await CreateActiveProductAsync();
        var request = await CreateSubmittedRequestAsync(product.Id);

        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{request.Id}/approve", null);

        await _client.AuthenticateAsInternalAsync();
        var reviewCreateResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/shariah-reviews", new CreateIslamicFinanceShariahReviewRequest
        {
            ReviewCaseNumber = $"SHR-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            ProductProfileId = product.Id,
            FinancingRequestId = request.Id,
            ReviewReasonCode = "SHARIAH_REVIEW"
        });

        var review = await reviewCreateResponse.Content.ReadFromJsonAsync<IslamicFinanceShariahReviewResponse>();
        Assert.NotNull(review);

        await _client.PostAsJsonAsync(
            $"/api/v1/islamic-finance/requests/{request.Id}/attach-shariah-review",
            new AttachIslamicFinanceShariahReviewRequest
            {
                ShariahReviewCaseId = review!.Id
            });

        var decisionId = SeedDecision(request.Id);

        var historyResponse = await _client.GetAsync($"/api/v1/islamic-finance/requests/{request.Id}/history");
        Assert.Equal(HttpStatusCode.OK, historyResponse.StatusCode);
        var history = await historyResponse.Content.ReadFromJsonAsync<IslamicFinanceRequestHistoryEntryResponse[]>();
        Assert.NotNull(history);
        Assert.Contains(history!, item => item.EventCode == "SHARIAH_REVIEW_ATTACHED");
        Assert.Contains(history!, item => item.EventCode == "DECISION_CREATED");

        var requestAuditResponse = await _client.GetAsync($"/api/v1/islamic-finance/requests/{request.Id}/audit");
        Assert.Equal(HttpStatusCode.OK, requestAuditResponse.StatusCode);
        var requestAudit = await requestAuditResponse.Content.ReadFromJsonAsync<IslamicFinanceRequestAuditResponse>();
        Assert.NotNull(requestAudit);
        Assert.Single(requestAudit!.Decisions);

        var decisionsResponse = await _client.GetAsync($"/api/v1/islamic-finance/requests/{request.Id}/decisions");
        Assert.Equal(HttpStatusCode.OK, decisionsResponse.StatusCode);
        var decisions = await decisionsResponse.Content.ReadFromJsonAsync<IslamicFinanceDecisionResponse[]>();
        Assert.NotNull(decisions);
        Assert.Single(decisions!);

        var decisionDetailResponse = await _client.GetAsync($"/api/v1/islamic-finance/decisions/{decisionId}");
        Assert.Equal(HttpStatusCode.OK, decisionDetailResponse.StatusCode);

        var decisionAuditResponse = await _client.GetAsync($"/api/v1/islamic-finance/decisions/{decisionId}/audit");
        Assert.Equal(HttpStatusCode.OK, decisionAuditResponse.StatusCode);
        var decisionAudit = await decisionAuditResponse.Content.ReadFromJsonAsync<IslamicFinanceDecisionAuditResponse>();
        Assert.NotNull(decisionAudit);
        Assert.Contains(decisionAudit!.Events, item => item.EventCode == "DECISION_APPROVED");
    }

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

        var partner = await CreateActivePartnerAsync();
        var product = await CreateActiveProductAsync();

        var facilityCreateResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/facilities", new CreateIslamicFinanceFacilityRequest
        {
            PartnerLenderId = partner.Id,
            CustomerPartyId = 1,
            ProductProfileId = product.Id,
            FacilityNumber = $"FAC-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            CurrencyCode = "EGP",
            ApprovedLimitAmount = 300000m,
            UtilizedAmount = 15000m
        });
        var facility = await facilityCreateResponse.Content.ReadFromJsonAsync<IslamicFinanceFacilityResponse>();
        Assert.NotNull(facility);
        await _client.PostAsync($"/api/v1/islamic-finance/facilities/{facility!.Id}/submit", null);
        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/facilities/{facility.Id}/approve", null);
        await _client.PostAsync($"/api/v1/islamic-finance/facilities/{facility.Id}/activate", null);

        await _client.AuthenticateAsInternalAsync();
        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/requests", new CreateIslamicFinanceFinancingRequestRequest
        {
            RequestNumber = $"REQ-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            CustomerPartyId = 1,
            ProductProfileId = product.Id,
            FacilityId = facility.Id,
            FundingSourceMode = "PARTNER",
            SourceDocumentType = "SALES_ORDER",
            SourceDocumentId = 2201,
            TransactionCurrencyCode = "EGP",
            RequestedAmount = 140000m
        });

        var request = await createResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(request);
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{request!.Id}/submit", null);

        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{request.Id}/approve", null);

        await _client.AuthenticateAsInternalAsync();
        var assignResponse = await _client.PostAsJsonAsync(
            $"/api/v1/islamic-finance/requests/{request.Id}/assign-partner-lender",
            new AssignIslamicFinancePartnerLenderRequest
            {
                PartnerLenderId = partner.Id
            });

        Assert.Equal(HttpStatusCode.OK, assignResponse.StatusCode);
        var assigned = await assignResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(assigned);
        Assert.Equal("ASSIGNED", assigned!.StatusCode);
        Assert.Equal(partner.Id, assigned.AssignedPartnerLenderId);

        var decisionCreateResponse = await _client.PostAsJsonAsync(
            $"/api/v1/islamic-finance/requests/{request.Id}/decisions",
            new CreateIslamicFinanceDecisionRequest
            {
                DecisionCode = "APPROVED",
                DecisionNotes = "Approved through HTTP workflow.",
                DecidingPartyId = 1,
                PartnerLenderId = partner.Id
            });

        Assert.Equal(HttpStatusCode.OK, decisionCreateResponse.StatusCode);
        var decision = await decisionCreateResponse.Content.ReadFromJsonAsync<IslamicFinanceDecisionResponse>();
        Assert.NotNull(decision);
        Assert.Equal("DRAFT", decision!.StatusCode);

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

        await _client.AuthenticateAsFinanceCheckerAsync();
        var approveDecisionResponse = await _client.PostAsync($"/api/v1/islamic-finance/decisions/{decision.Id}/approve", null);
        Assert.Equal(HttpStatusCode.OK, approveDecisionResponse.StatusCode);
        var approvedDecision = await approveDecisionResponse.Content.ReadFromJsonAsync<IslamicFinanceDecisionResponse>();
        Assert.NotNull(approvedDecision);
        Assert.Equal("APPROVED", approvedDecision!.StatusCode);
        Assert.Equal(partner.Id, approvedDecision.PartnerLenderId);
    }

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

        var product = await CreateActiveProductAsync();
        var request = await CreateSubmittedRequestAsync(product.Id);

        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/shariah-reviews", new CreateIslamicFinanceShariahReviewRequest
        {
            ReviewCaseNumber = $"SHR-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            ProductProfileId = product.Id,
            FinancingRequestId = request.Id,
            ReviewReasonCode = "SHARIAH_REVIEW"
        });

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

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

        var recommendResponse = await _client.PostAsJsonAsync(
            $"/api/v1/islamic-finance/shariah-reviews/{created.Id}/recommend-outcome",
            new RecommendIslamicFinanceShariahOutcomeRequest
            {
                ReviewOutcomeCode = "APPROVED",
                ReviewNotes = "Shariah review completed."
            });

        Assert.Equal(HttpStatusCode.OK, recommendResponse.StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();

        var approveResponse = await _client.PostAsync($"/api/v1/islamic-finance/shariah-reviews/{created.Id}/approve-outcome", null);
        Assert.Equal(HttpStatusCode.OK, approveResponse.StatusCode);
        var approved = await approveResponse.Content.ReadFromJsonAsync<IslamicFinanceShariahReviewResponse>();
        Assert.NotNull(approved);
        Assert.Equal("APPROVED", approved!.StatusCode);
        Assert.Equal("APPROVED", approved.ReviewOutcomeCode);

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

        var listResponse = await _client.GetAsync($"/api/v1/islamic-finance/shariah-reviews?statusCode=APPROVED&financingRequestId={request.Id}");
        Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
        var list = await listResponse.Content.ReadFromJsonAsync<IslamicFinanceShariahReviewResponse[]>();
        Assert.NotNull(list);
        Assert.Single(list!);
    }

    private async Task<IslamicFinancePartnerLenderResponse> CreateActivePartnerAsync()
    {
        using (var scope = _factory.Services.CreateScope())
        {
            var dbContext = scope.ServiceProvider.GetRequiredService<CashLineErpDbContext>();
            var partnerService = new IslamicFinancePartnerService(new IslamicFinancePartnerRepository(dbContext));
            var existing = partnerService.GetPartnerLenders(activeOnly: true).FirstOrDefault(entry => entry.PartnerLenderPartyId == 1);
            if (existing is not null)
            {
                return existing;
            }
        }

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

        var created = await createResponse.Content.ReadFromJsonAsync<IslamicFinancePartnerLenderResponse>();
        Assert.NotNull(created);

        await _client.PostAsync($"/api/v1/islamic-finance/partners/{created!.Id}/submit", null);
        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/partners/{created.Id}/approve", null);
        var activateResponse = await _client.PostAsync($"/api/v1/islamic-finance/partners/{created.Id}/activate", null);
        var activated = await activateResponse.Content.ReadFromJsonAsync<IslamicFinancePartnerLenderResponse>();
        Assert.NotNull(activated);
        await _client.AuthenticateAsInternalAsync();
        return activated!;
    }

    private async Task<IslamicFinanceProductProfileResponse> CreateActiveProductAsync()
    {
        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/product-profiles", new CreateIslamicFinanceProductProfileRequest
        {
            ProductType = "MURABAHA",
            ProductCode = $"MUR-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            ProductName = "Murabaha Standard",
            RequiresShariahReview = true,
            AllowsPartnerFunding = true,
            AllowsCashLineFunding = false
        });

        var created = await createResponse.Content.ReadFromJsonAsync<IslamicFinanceProductProfileResponse>();
        Assert.NotNull(created);

        await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created!.Id}/submit", null);
        await _client.AuthenticateAsFinanceCheckerAsync();
        await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}/approve", null);
        var activateResponse = await _client.PostAsync($"/api/v1/islamic-finance/product-profiles/{created.Id}/activate", null);
        var activated = await activateResponse.Content.ReadFromJsonAsync<IslamicFinanceProductProfileResponse>();
        Assert.NotNull(activated);
        await _client.AuthenticateAsInternalAsync();
        return activated!;
    }

    private async Task<IslamicFinanceFinancingRequestResponse> CreateSubmittedRequestAsync(long productProfileId)
    {
        var createResponse = await _client.PostAsJsonAsync("/api/v1/islamic-finance/requests", new CreateIslamicFinanceFinancingRequestRequest
        {
            RequestNumber = $"REQ-{Guid.NewGuid():N}"[..12].ToUpperInvariant(),
            CustomerPartyId = 1,
            ProductProfileId = productProfileId,
            FundingSourceMode = "PARTNER",
            SourceDocumentType = "SALES_ORDER",
            SourceDocumentId = 2001,
            TransactionCurrencyCode = "EGP",
            RequestedAmount = 120000m
        });

        var created = await createResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(created);
        await _client.PostAsync($"/api/v1/islamic-finance/requests/{created!.Id}/submit", null);
        var detailResponse = await _client.GetAsync($"/api/v1/islamic-finance/requests/{created.Id}");
        var detail = await detailResponse.Content.ReadFromJsonAsync<IslamicFinanceFinancingRequestResponse>();
        Assert.NotNull(detail);
        return detail!;
    }

    private long SeedDecision(long financingRequestId)
    {
        using var scope = _factory.Services.CreateScope();
        var dbContext = scope.ServiceProvider.GetRequiredService<CashLineErpDbContext>();
        var repository = new CashLineERP.Infrastructure.Persistence.Repositories.IslamicFinance.IslamicFinanceRequestRepository(dbContext);

        var decision = repository.CreateDecision(new CashLineERP.Application.IslamicFinance.Requests.CreateIslamicFinanceDecisionCommand(
            financingRequestId,
            "APPROVED",
            "Approved for audit timeline.",
            1,
            null,
            "MURABAHA",
            "PARTNER",
            "EGP",
            120000m,
            12,
            8.0m,
            1000m,
            "Corporate guarantee",
            1));
        repository.SubmitDecision(decision.FinancingDecisionId, 3);
        repository.ApproveDecision(decision.FinancingDecisionId, 4);
        return decision.FinancingDecisionId;
    }
}
