<?php

it('wires mobile stripe payment intent routes to normalized lifecycle handling', function (): void {
    $routeFile = app_path('routes/ordersRoute.php');
    $content = file_get_contents($routeFile);

    expect($content)->not->toBeFalse();

    $start = strpos($content, "\$this->post('/orders/module/stripe/payment_intent'");
    $end = strpos($content, "\$this->post('/orders/module/stripe/debug/simulate_payment'");
    expect($start)->not->toBeFalse();
    expect($end)->not->toBeFalse();

    $stripeSection = substr($content, (int)$start, (int)$end - (int)$start);

    expect($stripeSection)->toContain('buildStripePaymentIntentResponse(');
    expect($stripeSection)->toContain('$expectedPaymentIntentAmount = $this->getStripePaymentIntentAmountForOrder($order, $tax_percentage ?? 0);');
    expect($stripeSection)->toContain('isStripePaymentIntentReusable($storedPaymentIntent)');
    expect($stripeSection)->toContain('doesStripePaymentIntentMatchOrder($storedPaymentIntent, $expectedPaymentIntentAmount, $tax_percentage ?? 0)');
    expect($stripeSection)->toContain('$stripe->payment_intents->create(
                $expectedPaymentIntentAmount,');
    expect($stripeSection)->toContain("'reused' => true");
    expect($stripeSection)->toContain("'message' => 'No active payment intent for this order.'");
    expect($stripeSection)->toContain("'message' => 'Payment intent cleared successfully.'");
    expect($stripeSection)->toContain("Stored payment intent is stale. Start the payment again.");
    expect($stripeSection)->toContain("Payment intent has already been captured.");
    expect($stripeSection)->toContain("Payment intent was cancelled. Start the payment again.");
    expect($stripeSection)->toContain("Payment intent is not ready to capture.");
    expect($stripeSection)->toContain("paidWithStripe(\$paymentIntent->id);");
    expect($stripeSection)->not->toContain("Order does not have a payment intent");

    expect($content)->toContain('private function doesStripePaymentIntentMatchOrder(object $paymentIntent, int $expectedAmount, ?int $tax_percentage): bool');
    expect($content)->toContain('!isset($paymentIntent->amount) || (int)$paymentIntent->amount !== $expectedAmount');
    expect($content)->toContain("\$storedTaxPercentage = \$metadata['tax_percentage'] ?? null;");
});
