using System.Diagnostics;
using CashLineERP.Application.Tax;
using CashLineERP.Application.External;
using CashLineERP.Application.Finance;
using CashLineERP.Application.Internal;
using CashLineERP.Application.IslamicFinance.Disbursements;
using CashLineERP.Application.IslamicFinance.ProductProfiles;
using CashLineERP.Application.IslamicFinance.ReceivablesFinance;
using CashLineERP.Application.IslamicFinance.Requests;
using CashLineERP.Application.IslamicFinance.Settlements;
using CashLineERP.Application.Notifications;
using CashLineERP.Application.Parties;
using CashLineERP.Application.Reporting;
using CashLineERP.Application.Sales;
using CashLineERP.Application.Suppliers;
using CashLineERP.Application.Customers;
using CashLineERP.Contracts.Finance;
using CashLineERP.Infrastructure.Persistence.Repositories;
using CashLineERP.Infrastructure.Persistence.Repositories.IslamicFinance;

namespace CashLineERP.Api.Tests;

public sealed class NonFunctionalSmokeQaTests
{
    [Fact]
    public void Read_Model_Smoke_Workload_Completes_Within_Baseline_Time_Window()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var reportingService = new CoreReportingService(new CoreReportingRepository(dbContext));
        var internalWorkbenchService = new InternalWorkbenchService(new InternalWorkbenchRepository(dbContext));
        var supplierProfileService = new SupplierProfileService(new SupplierProfileRepository(dbContext));
        var customerProfileService = new CustomerProfileService(new CustomerProfileRepository(dbContext));
        var partyOnboardingService = new PartyOnboardingService(new BusinessPartyRepository(dbContext));
        var salesOrderService = new SalesOrderService(new SalesOrderRepository(dbContext));
        var islamicFinanceProductService = new IslamicFinanceProductService(new IslamicFinanceProductRepository(dbContext));
        var islamicFinanceRequestService = new IslamicFinanceRequestService(new IslamicFinanceRequestRepository(dbContext));
        var receivablesFinanceService = new ReceivablesFinanceService(new ReceivablesFinanceRepository(dbContext));
        var disbursementService = new IslamicFinanceMurabahaDisbursementService(new IslamicFinanceMurabahaDisbursementRepository(dbContext));
        var settlementService = new IslamicFinanceSettlementService(new IslamicFinanceSettlementRepository(dbContext));
        var notificationTemplateService = new NotificationTemplateService(new NotificationTemplateRepository(dbContext));
        var notificationOrchestrationService = new NotificationOrchestrationService(new NotificationOrchestrationRepository(dbContext));
        var paymentGatewayExecutionService = new PaymentGatewayExecutionService(new PaymentGatewayExecutionRepository(dbContext));
        var externalPortalService = new ExternalPortalService(
            new ExternalPortalRepository(dbContext),
            supplierProfileService,
            customerProfileService,
            partyOnboardingService,
            salesOrderService,
            islamicFinanceProductService,
            islamicFinanceRequestService,
            receivablesFinanceService,
            disbursementService,
            settlementService,
            notificationTemplateService,
            notificationOrchestrationService,
            paymentGatewayExecutionService);

        var stopwatch = Stopwatch.StartNew();

        for (var index = 0; index < 50; index++)
        {
            reportingService.GetCoreFinanceReport(new DateOnly(2026, 4, 1), new DateOnly(2026, 4, 30));
            reportingService.GetCoreOperationsReport(new DateOnly(2026, 4, 1), new DateOnly(2026, 4, 30));
            internalWorkbenchService.GetDashboardSummary();
            internalWorkbenchService.GetApprovalQueue();
            externalPortalService.GetStatementSummary(1);
            externalPortalService.GetStatementSummary(2);
        }

        stopwatch.Stop();

        Assert.True(
            stopwatch.Elapsed < TimeSpan.FromSeconds(5),
            $"Read-model smoke workload exceeded baseline threshold. Elapsed: {stopwatch.Elapsed}.");
    }

    [Fact]
    public void Reconciliation_Exception_Queue_Smoke_Workload_Completes_Within_Baseline_Time_Window()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var reconciliationService = new ReconciliationService(new ReconciliationRepository(dbContext));

        var stopwatch = Stopwatch.StartNew();

        for (var index = 0; index < 100; index++)
        {
            reconciliationService.GetExceptionQueue();
        }

        stopwatch.Stop();

        Assert.True(
            stopwatch.Elapsed < TimeSpan.FromSeconds(3),
            $"Reconciliation exception-queue smoke workload exceeded baseline threshold. Elapsed: {stopwatch.Elapsed}.");
    }

    [Fact]
    public void Month_End_And_Eta_Control_Reads_Complete_Within_Baseline_Time_Window()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var invoiceService = new AccountsReceivableInvoiceService(new AccountsReceivableInvoiceRepository(dbContext));
        var accountingPeriodService = new AccountingPeriodService(new AccountingPeriodRepository(dbContext));
        var taxService = new StatutoryTaxService(new StatutoryTaxRepository(dbContext));
        var etaService = new EtaComplianceService(new EtaComplianceRepository(dbContext));

        var createdInvoice = invoiceService.CreateInvoice(new CreateAccountsReceivableInvoiceRequest(
            CustomerId: 2,
            SalesOrderId: 1,
            DispatchOrderId: 1,
            InvoiceType: "TAX",
            InvoiceDate: new DateOnly(2026, 4, 18),
            TaxDate: new DateOnly(2026, 4, 18),
            DueDate: new DateOnly(2026, 4, 28),
            CurrencyCode: "EGP",
            Lines:
            [
                new CreateAccountsReceivableInvoiceLineRequest(1, 1001, 2m, "EA", 32m, "VAT14")
            ]));
        var postedInvoice = AccountsReceivableWorkflowTestHelper.SubmitApproveAndPostInvoice(invoiceService, createdInvoice.AccountsReceivableInvoiceId);

        var stopwatch = Stopwatch.StartNew();

        for (var index = 0; index < 50; index++)
        {
            accountingPeriodService.GetCloseReadiness(1);
            accountingPeriodService.GetMonthEndReview(1);
            etaService.GetPayloadPreview(postedInvoice.AccountsReceivableInvoiceId);
            etaService.GetSubmissionValidationPack(postedInvoice.AccountsReceivableInvoiceId);
            taxService.GetAccountsReceivableInvoiceVatBreakdownExport(postedInvoice.AccountsReceivableInvoiceId);
            taxService.GetAccountsReceivableInvoiceFinanceReviewPackage(postedInvoice.AccountsReceivableInvoiceId);
        }

        stopwatch.Stop();

        Assert.True(
            stopwatch.Elapsed < TimeSpan.FromSeconds(6),
            $"Month-end and ETA control smoke workload exceeded baseline threshold. Elapsed: {stopwatch.Elapsed}.");
    }
}
