<?php

namespace Database\Seeders;

use App\Models\Application;
use App\Models\ImportedJob;
use App\Models\Profile;
use App\Models\QuestionnaireResponse;
use App\Models\ResumeReport;
use App\Models\SavedJob;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DemoDataSeeder extends Seeder
{
    public function run(): void
    {
        $this->call(ImportedJobSeeder::class);

        $user = User::query()->updateOrCreate(
            ['email' => 'applicant@example.com'],
            [
                'name' => 'Demo Applicant',
                'username' => 'demo-applicant',
                'password' => Hash::make('Applicant#2026!'),
                'password_set_at' => now(),
                'email_verified_at' => now(),
                'role' => 'user',
                'questionnaire_completed_at' => now()->subHours(4),
                'access_unlocked_at' => now()->subHours(3),
                'resume_uploaded_at' => now()->subHours(2),
                'resume_analyzed_at' => now()->subHour(),
                'profile_completed_at' => now()->subMinutes(45),
            ]
        );

        QuestionnaireResponse::query()->updateOrCreate(
            ['user_id' => $user->id],
            [
                'answers' => [
                    'preferred_roles' => ['Product Operations', 'Customer Support', 'Content Writing'],
                    'categories' => ['operations', 'support', 'writing'],
                    'experience_level' => 'mid',
                    'remote_preference' => 'fully_remote',
                ],
                'preferred_categories' => ['operations', 'support', 'writing'],
                'preferred_roles' => ['Product Operations Associate', 'Customer Support Specialist', 'Content Writer'],
                'skills' => ['remote communication', 'documentation', 'customer support', 'content writing'],
                'experience_level' => 'mid',
                'availability' => 'immediate',
                'preferred_timezone' => 'UTC',
                'salary_currency' => 'USD',
                'salary_min' => 35000,
                'open_to_contract' => true,
                'open_to_full_time' => true,
                'completed_at' => now()->subHours(4),
            ]
        );

        ResumeReport::query()->updateOrCreate(
            ['user_id' => $user->id, 'is_current' => true],
            [
                'original_filename' => 'demo-resume.pdf',
                'disk' => 'local',
                'path' => 'demo/demo-resume.pdf',
                'mime_type' => 'application/pdf',
                'size_bytes' => 132000,
                'status' => 'completed',
                'ats_score' => 78,
                'remote_readiness_score' => 82,
                'extracted_skills' => ['remote communication', 'operations', 'documentation', 'customer support', 'content writing'],
                'role_suggestions' => ['Product Operations Associate', 'Customer Support Specialist', 'Content Writer'],
                'missing_keywords' => ['SLA management', 'CRM reporting', 'workflow automation'],
                'resume_improvements' => ['Add quantified outcomes to recent work experience.', 'Include tools used for distributed collaboration.'],
                'analysis' => [
                    'ats_score' => 78,
                    'remote_readiness_score' => 82,
                    'experience_level' => 'mid',
                ],
                'analyzed_at' => now()->subHour(),
            ]
        );

        Profile::query()->updateOrCreate(
            ['user_id' => $user->id],
            [
                'headline' => 'Remote operations and support specialist',
                'summary' => 'Demo applicant profile for marketplace preview data.',
                'current_role' => 'Operations Specialist',
                'country_code' => 'US',
                'city' => 'Remote',
                'timezone' => 'UTC',
                'preferred_roles' => ['Product Operations Associate', 'Customer Support Specialist', 'Content Writer'],
                'preferred_categories' => ['operations', 'support', 'writing'],
                'skills' => ['remote communication', 'documentation', 'support', 'content writing'],
                'experience_level' => 'mid',
                'availability' => 'immediate',
                'salary_currency' => 'USD',
                'salary_min' => 35000,
                'open_to_contract' => true,
                'open_to_full_time' => true,
                'completed_at' => now()->subMinutes(45),
            ]
        );

        $jobs = ImportedJob::query()->active()->latest('posted_at')->limit(2)->get();

        foreach ($jobs as $index => $job) {
            SavedJob::query()->firstOrCreate([
                'user_id' => $user->id,
                'imported_job_id' => $job->id,
            ]);

            if ($index === 0) {
                Application::query()->firstOrCreate(
                    [
                        'user_id' => $user->id,
                        'imported_job_id' => $job->id,
                    ],
                    [
                        'status' => 'applied',
                        'job_source_id' => $job->job_source_id,
                        'source_url' => $job->source_url ?: url('/'),
                        'apply_url' => $job->apply_url ?: ($job->source_url ?: url('/')),
                        'source_attribution' => $job->attribution(),
                        'ip_address' => '127.0.0.1',
                        'user_agent' => 'RemoteMatch demo seeder',
                        'applied_at' => now()->subMinutes(30),
                    ]
                );
            }
        }
    }
}
