<?php

namespace Tests\Feature\Applicants;

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

class ApplicantDashboardTest extends TestCase
{
    use RefreshDatabase;

    public function test_applicant_dashboard_renders_analytics_and_top_matches(): void
    {
        $user = $this->unlockedUser();
        $job = $this->remoteJob([
            'title' => 'Remote Customer Success Manager',
            'category' => 'support',
            'skills' => ['Customer success', 'CRM', 'Documentation'],
            'salary_currency' => 'USD',
            'salary_min' => 4500,
            'salary_max' => 6500,
        ]);
        $lowerMatchJob = $this->remoteJob([
            'external_id' => 'lower-match-job',
            'title' => 'Remote Finance Analyst',
            'category' => 'finance',
            'skills' => ['Accounting'],
            'salary_currency' => 'USD',
            'salary_min' => 3000,
            'salary_max' => 5000,
        ]);

        $response = $this->actingAs($user)->get(route('applicant.dashboard'));

        $response
            ->assertOk()
            ->assertSee('Applicant workspace')
            ->assertSee('Matched jobs')
            ->assertSee('ATS score')
            ->assertSee('Remote Customer Success Manager')
            ->assertSee('Remote Finance Analyst')
            ->assertSee(route('jobs.show', $job), false)
            ->assertSee(route('jobs.show', $lowerMatchJob), false)
            ->assertSee(route('jobs.index'), false)
            ->assertSee(route('jobs.index', ['saved' => 1]), false)
            ->assertSee(route('jobs.index', ['applied' => 1]), false)
            ->assertSee(route('jobs.index', ['match_min' => 75]), false)
            ->assertSee('$4,500 - $6,500')
            ->assertSee('salary-badge', false)
            ->assertDontSee($job->apply_url, false)
            ->assertDontSee($job->source_url, false);
    }

    private function unlockedUser(): User
    {
        $user = User::factory()->create([
            'questionnaire_completed_at' => now(),
            'access_unlocked_at' => now(),
            'resume_uploaded_at' => now(),
            'resume_analyzed_at' => now(),
            'profile_completed_at' => now(),
        ]);

        QuestionnaireResponse::query()->create([
            'user_id' => $user->id,
            'answers' => ['goal' => 'Find remote support roles'],
            'preferred_categories' => ['support'],
            'preferred_roles' => ['Customer Success Manager'],
            'skills' => ['Customer success', 'CRM', 'Documentation'],
            'experience_level' => 'mid',
            'availability' => 'immediate',
            'country_code' => 'IN',
            'salary_currency' => 'USD',
            'salary_min' => 4000,
            'open_to_contract' => false,
            'open_to_full_time' => true,
            'completed_at' => now(),
        ]);

        ResumeReport::query()->create([
            'user_id' => $user->id,
            'original_filename' => 'resume.pdf',
            'disk' => 'local',
            'path' => 'resumes/'.$user->id.'/resume.pdf',
            'mime_type' => 'application/pdf',
            'size_bytes' => 4096,
            'status' => ResumeReport::STATUS_COMPLETED,
            'ats_score' => 86,
            'remote_readiness_score' => 82,
            'extracted_skills' => ['Customer success', 'CRM', 'Documentation'],
            'role_suggestions' => ['Customer Success Manager'],
            'missing_keywords' => ['Retention'],
            'resume_improvements' => ['Add measurable retention outcomes.'],
            'analysis' => ['summary' => 'Strong remote customer success profile.'],
            'analyzed_at' => now(),
        ]);

        Profile::query()->create([
            'user_id' => $user->id,
            'headline' => 'Remote customer success manager',
            'summary' => 'Customer success leader for remote teams.',
            'current_role' => 'Customer Success Manager',
            'experience_level' => 'mid',
            'country_code' => 'IN',
            'timezone' => 'Asia/Kolkata',
            'skills' => ['Customer success', 'CRM', 'Documentation'],
            'preferred_roles' => ['Customer Success Manager'],
            'preferred_categories' => ['support'],
            'availability' => 'immediate',
            'salary_currency' => 'USD',
            'salary_min' => 4000,
            'open_to_contract' => false,
            'open_to_full_time' => true,
            'completed_at' => now(),
        ]);

        return $user;
    }

    /**
     * @param  array<string, mixed>  $overrides
     */
    private function remoteJob(array $overrides = []): ImportedJob
    {
        $source = JobSource::query()->firstOrCreate([
            'slug' => 'applicant-test-source',
        ], [
            'name' => 'Applicant Test Source',
            'type' => 'manual',
            'base_url' => 'https://source.example',
            'attribution_text' => 'Source: Applicant Test Source',
            'is_active' => true,
        ]);

        return ImportedJob::query()->create(array_replace([
            'job_source_id' => $source->id,
            'external_id' => 'applicant-job-001',
            'title' => 'Remote Operations Coordinator',
            'company' => 'Distributed Team Co',
            'category' => 'operations',
            'employment_type' => 'full_time',
            'experience_level' => 'mid',
            'location_type' => 'remote',
            'country_code' => 'IN',
            'summary' => 'Remote-first role.',
            'description' => 'Remote-first role description.',
            'skills' => ['Operations'],
            'tags' => ['remote'],
            'source_url' => 'https://source.example/jobs/applicant-job-001',
            'apply_url' => 'https://source.example/jobs/applicant-job-001/apply',
            'source_attribution' => 'Source: Applicant Test Source',
            'status' => ImportedJob::STATUS_ACTIVE,
            'posted_at' => now(),
            'last_seen_at' => now(),
        ], $overrides));
    }
}
