<?php

it('registers all collected-invoice economic v2 routes with explicit permissions', function (): void {
    $routeFile = app_path('routes/orderInvoicesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/collected-invoices/economic/v2/details');
    expect($content)->toContain('/collected-invoices/economic/v2/compare');
    expect($content)->toContain('/collected-invoices/economic/v2/compare/bulk');
    expect($content)->toContain('/collected-invoices/economic/v2/revenue-statistics');
    expect($content)->toContain("requirePermission('view_collected_invoice_economic_v2_details')");
    expect($content)->toContain("requirePermission('compare_collected_invoice_economic_v2')");
    expect($content)->toContain("requirePermission('compare_collected_invoice_economic_v2_bulk')");
    expect($content)->toContain("requirePermission('view_collected_invoice_economic_v2_revenue_statistics')");
    expect($content)->toContain("requireParameters(['collected_invoice_ids'])");
});

it('registers version-aware distribution and pricing history v2 routes', function (): void {
    $routeFile = app_path('routes/InvoicingPeriodRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/all');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/fixed-pricing');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/wash-subscriptions');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/customer-prices');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/booked-department-75');
    expect($content)->toContain('/superuser/customers/pricing-history');
    expect($content)->toContain("requirePermission('superuser_invoicing_period_distribution_v2')");
    expect($content)->toContain("requirePermission('superuser_customer_pricing_history_v2')");
});

it('caches v2 distribution responses with a configurable redis ttl', function (): void {
    $routeFile = app_path('routes/InvoicingPeriodRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('withCachedDistributionV2(');
    expect($content)->toContain('invoicing_period:distribution:v2:');
    expect($content)->toContain('INVOICING_PERIOD_DISTRIBUTION_V2_CACHE_TTL');
});

it('writes fixed pricing versions from create and delete flows', function (): void {
    $routeFile = app_path('routes/customerFixedPricingRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('recordFixedPricingVersion(');
    expect($content)->toContain('closeActiveFixedPricingVersion(');
});

it('writes vehicle subscription versions for create update delete flows', function (): void {
    $routeFile = app_path('routes/vehiclesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('recordVehicleSubscriptionVersion(');
    expect($content)->toContain('closeActiveVehicleSubscriptionVersion(');
    expect($content)->toContain("if (self::isParametersSet(['customer_id']))");
});

it('writes discount override versions from superuser discounts route', function (): void {
    $routeFile = app_path('routes/userRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/superuser/user/discounts');
    expect($content)->toContain('recordDiscountOverrideVersion(');
});

it('keeps legacy compare endpoint path for backward compatibility', function (): void {
    $routeFile = app_path('routes/orderInvoicesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/collected-invoices/economic/compare');
    expect($content)->toContain("requirePermission('compare_collected_invoice_economic')");
});

it('enforces customer or department context before building v2 invoice details payload', function (): void {
    $routeFile = app_path('routes/orderInvoicesRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('private function requireCollectedInvoiceContextAccess');
    expect($content)->toContain('$this->requireCollectedInvoiceContextAccess($invoice);');
    expect($content)->toContain("$this->hasPermission('superuser')");
    expect($content)->toContain('$this->isOwnCustomerContext($invoice_customer_number)');
    expect($content)->toContain('$this->hasAccessToAllCollectedInvoiceDepartments((int)$invoice->id)');
});
