<?php

namespace Tests\Feature\Admin;

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

class AdminDashboardAndPreviewPagesTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_dashboard_renders_analytics_widgets(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this
            ->actingAs($admin, 'admin')
            ->get('/admin')
            ->assertOk()
            ->assertSee('Admin command center')
            ->assertSee('Operational snapshot')
            ->assertSee('Growth and activation trend')
            ->assertSee('Payment revenue')
            ->assertSee('Active jobs by category')
            ->assertSee('Recent application activity');
    }

    public function test_admin_can_open_user_application_and_questionnaire_preview_pages(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

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

        $questionnaire = QuestionnaireResponse::query()->create([
            'user_id' => $applicant->id,
            'answers' => ['mode' => 'multiple_choice'],
            'preferred_categories' => ['tech', 'marketing'],
            'preferred_roles' => ['Product Manager'],
            'skills' => ['Remote communication', 'SEO'],
            'experience_level' => 'mid',
            'availability' => 'immediate',
            'preferred_timezone' => 'Asia/Kolkata',
            'country_code' => 'IN',
            'salary_currency' => 'USD',
            'salary_min' => 2500,
            'open_to_contract' => true,
            'open_to_full_time' => true,
            'completed_at' => now(),
        ]);

        $source = JobSource::query()->create([
            'name' => 'Manual Source',
            'slug' => 'manual-source',
            'type' => 'manual',
            'base_url' => 'https://example.com',
            'feed_url' => null,
            'attribution_text' => 'Sourced from Example',
            'is_active' => true,
        ]);

        $job = ImportedJob::query()->create([
            'job_source_id' => $source->id,
            'external_id' => 'job-1',
            'title' => 'Remote Product Manager',
            'company' => 'Example Co',
            'category' => 'product',
            'employment_type' => 'full_time',
            'location_type' => 'remote',
            'salary_currency' => 'USD',
            'salary_min' => 5000,
            'salary_max' => 7000,
            'summary' => 'Remote product role.',
            'description' => 'Remote product role description.',
            'skills' => ['Product strategy', 'Remote communication'],
            'tags' => ['remote', 'product'],
            'source_url' => 'https://example.com/jobs/1',
            'apply_url' => 'https://example.com/apply/1',
            'source_attribution' => 'Example jobs',
            'status' => ImportedJob::STATUS_ACTIVE,
            'posted_at' => now(),
            'last_seen_at' => now(),
        ]);

        $application = Application::query()->create([
            'user_id' => $applicant->id,
            'imported_job_id' => $job->id,
            'job_source_id' => $source->id,
            'source_url' => $job->source_url,
            'apply_url' => $job->apply_url,
            'source_attribution' => $job->source_attribution,
            'status' => Application::STATUS_APPLIED,
            'ip_address' => '127.0.0.1',
            'user_agent' => 'Feature test',
            'applied_at' => now(),
        ]);

        $this
            ->actingAs($admin, 'admin')
            ->get("/admin/users/{$applicant->id}")
            ->assertOk()
            ->assertSee('Applicant overview')
            ->assertSee($applicant->email);

        $this
            ->actingAs($admin, 'admin')
            ->get("/admin/applications/{$application->id}")
            ->assertOk()
            ->assertSee('Application tracking')
            ->assertSee('Remote Product Manager');

        $this
            ->actingAs($admin, 'admin')
            ->get("/admin/questionnaire-responses/{$questionnaire->id}")
            ->assertOk()
            ->assertSee('Structured answers')
            ->assertSee('Product Manager');
    }

    public function test_admin_can_render_conditional_job_source_form(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $source = JobSource::query()->create([
            'name' => 'Configurable JSON Feed',
            'slug' => 'configurable-json-feed',
            'type' => JobSource::TYPE_JSON,
            'feed_url' => 'https://feeds.example.test/jobs.json',
            'request_method' => 'get',
            'auth_type' => JobSource::AUTH_NONE,
            'sync_frequency' => 'hourly',
            'is_active' => true,
            'sync_settings' => [
                'items_path' => 'jobs',
                'timeout' => 20,
            ],
        ]);

        $this
            ->actingAs($admin, 'admin')
            ->get("/admin/job-sources/{$source->id}/edit")
            ->assertOk()
            ->assertSee('Endpoint and schedule')
            ->assertSee('JSON/API mapping')
            ->assertSee('Request options');
    }
}
