<?php

namespace Tests\Feature\Payments;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PaymentAccessProtectionTest extends TestCase
{
    use RefreshDatabase;

    public function test_checkout_requires_completed_questionnaire(): void
    {
        $user = User::factory()->create(['questionnaire_completed_at' => null]);

        $response = $this->actingAs($user)->get(route('payment.checkout'));

        $response->assertRedirect(route('questionnaire.show'));
    }

    public function test_order_creation_requires_completed_questionnaire(): void
    {
        $user = User::factory()->create(['questionnaire_completed_at' => null]);

        $response = $this->actingAs($user)->postJson(route('payment.order'));

        $response->assertRedirect(route('questionnaire.show'));
    }

    public function test_order_creation_fails_when_razorpay_is_not_configured(): void
    {
        config([
            'services.razorpay.key' => null,
            'services.razorpay.secret' => null,
            'services.razorpay.unlock_amount_paise' => 199900,
        ]);

        $user = User::factory()->create(['questionnaire_completed_at' => now()]);

        $response = $this->actingAs($user)->postJson(route('payment.order'));

        $response->assertStatus(503);
        $this->assertDatabaseCount('payments', 0);
    }
}
