<?php

namespace Tests\Feature\Tenant;

use App\Models\CheckoutSession;
use App\Models\Package;
use Illuminate\Support\Str;

class TenantCheckoutSessionStatusTest extends TenantTestCase
{
    public function test_tenant_can_fetch_checkout_session_status(): void
    {
        $package = Package::factory()->create([
            'price' => 129,
        ]);

        $session = CheckoutSession::create([
            'id' => (string) Str::uuid(),
            'user_id' => $this->tenantUser->id,
            'tenant_id' => $this->tenant->id,
            'package_id' => $package->id,
            'status' => CheckoutSession::STATUS_FAILED,
            'provider' => CheckoutSession::PROVIDER_PADDLE,
            'provider_metadata' => [
                'paddle_checkout_url' => 'https://checkout.paddle.test/checkout/123',
            ],
            'status_history' => [
                [
                    'status' => CheckoutSession::STATUS_FAILED,
                    'reason' => 'paddle_failed',
                    'at' => now()->toIso8601String(),
                ],
            ],
        ]);

        $response = $this->authenticatedRequest(
            'GET',
            "/api/v1/tenant/packages/checkout-session/{$session->id}/status"
        );

        $response->assertOk()
            ->assertJsonPath('status', CheckoutSession::STATUS_FAILED)
            ->assertJsonPath('reason', 'paddle_failed')
            ->assertJsonPath('checkout_url', 'https://checkout.paddle.test/checkout/123');
    }
}
