<?php

namespace Tests\Unit;

use App\Services\Stripe\PaymentAmountResolver;
use Tests\TestCase;

class PaymentAmountResolverTest extends TestCase
{
    public function test_resolves_amount_due_now_from_payment_step(): void
    {
        $resolver = new PaymentAmountResolver();
        $data = [
            ['step_3' => 'payment_details', 'amount_due_now' => 25, 'final_price' => 25, 'total_program_price' => 180],
            ['class_details' => ['price' => 25, 'total_program_price' => 180]],
        ];

        $resolved = $resolver->resolveFromSubmissionData($data);

        $this->assertSame(25.0, $resolved['total']);
        $this->assertSame(2500, $resolved['amount_cents']);
    }

    public function test_resolves_final_price_from_payment_step(): void
    {
        $resolver = new PaymentAmountResolver();
        $resolved = $resolver->resolveFromSubmissionData([
            ['step_1' => 'parent_details', 'email' => 'parent@example.com'],
            ['step_3' => 'payment_details', 'final_price' => 99.5],
            ['class_details' => ['price' => 100, 'invoice_tax_value' => 0]],
        ], 'usd');

        $this->assertSame(99.5, $resolved['total']);
        $this->assertSame(9950, $resolved['amount_cents']);
        $this->assertFalse($resolved['is_setup']);
    }

    public function test_rejects_mismatched_client_amount(): void
    {
        $resolver = new PaymentAmountResolver();
        $resolved = $resolver->resolveFromSubmissionData([
            ['final_price' => 50],
        ]);

        $this->expectException(\InvalidArgumentException::class);
        $resolver->assertClientAmountMatches($resolved, 10.0);
    }

    public function test_setup_when_total_is_zero(): void
    {
        $resolver = new PaymentAmountResolver();
        $resolved = $resolver->resolveFromSubmissionData([
            ['final_price' => 0],
        ]);

        $this->assertTrue($resolved['is_setup']);
        $this->assertSame(0, $resolved['amount_cents']);
    }
}
