<?php

namespace Tests\Feature\Payments;

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

class CheckoutLockedPreviewTest extends TestCase
{
    use RefreshDatabase;

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

        $user = User::factory()->create([
            'questionnaire_completed_at' => now(),
            'access_unlocked_at' => null,
        ]);
        $job = $this->remoteJob();

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

        $response
            ->assertOk()
            ->assertSee('Locked preview')
            ->assertSee('Actual job details and apply links stay hidden until payment and resume analysis are complete.')
            ->assertSee('Unlock and continue')
            ->assertDontSee('199900')
            ->assertDontSee('1,999')
            ->assertDontSee($job->title)
            ->assertDontSee($job->company)
            ->assertDontSee($job->source_url, false)
            ->assertDontSee($job->apply_url, false);
    }

    private function remoteJob(): ImportedJob
    {
        $source = JobSource::query()->create([
            'name' => 'Sensitive Source',
            'slug' => 'sensitive-source',
            'type' => 'manual',
            'base_url' => 'https://sensitive-source.example',
            'attribution_text' => 'Source: Sensitive Source',
            'is_active' => true,
        ]);

        return ImportedJob::query()->create([
            'job_source_id' => $source->id,
            'external_id' => 'hidden-job-001',
            'title' => 'Hidden Remote Product Lead',
            'company' => 'Stealth Hiring Co',
            'category' => 'product',
            'employment_type' => 'full_time',
            'experience_level' => 'senior',
            'location_type' => 'remote',
            'summary' => 'This real job summary must not be visible in checkout.',
            'description' => 'This real job description must stay locked before payment.',
            'skills' => ['Product strategy', 'Roadmapping'],
            'tags' => ['remote', 'product'],
            'source_url' => 'https://sensitive-source.example/jobs/hidden-job-001',
            'apply_url' => 'https://sensitive-source.example/jobs/hidden-job-001/apply?token=locked-secret',
            'source_attribution' => 'Source: Sensitive Source',
            'status' => ImportedJob::STATUS_ACTIVE,
            'posted_at' => now(),
            'last_seen_at' => now(),
        ]);
    }
}
