<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Worker;
use App\Models\WorkerCategory;
use App\Models\JobOffer;
use App\Models\WorkerDocument;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class EmployerWorkerWebFilterTest extends TestCase
{
    use RefreshDatabase;

    protected $employer;
    protected $category;

    protected function setUp(): void
    {
        parent::setUp();

        $this->employer = User::create([
            'name' => 'Test Employer',
            'email' => 'employer@example.com',
            'password' => bcrypt('password'),
            'role' => 'employer',
        ]);

        $this->category = WorkerCategory::create([
            'name' => 'General Helper',
        ]);

        // Seed some workers
        $w1 = Worker::create([
            'name' => 'Worker India Dubai',
            'email' => 'worker1@example.com',
            'phone' => '+971501111111',
            'password' => bcrypt('password'),
            'nationality' => 'India',
            'category_id' => $this->category->id,
            'verified' => true,
            'status' => 'active',
            'preferred_location' => 'Dubai',
            'preferred_job_type' => 'full-time',
            'live_in_out' => 'live_in',
            'in_country' => true,
            'visa_status' => 'Residence Visa',
            'age' => 25,
            'salary' => 1500,
            'experience' => '3 Years',
            'religion' => 'Christian',
            'language' => 'EN',
            'availability' => 'Immediate',
            'bio' => 'Experienced helper',
        ]);
        WorkerDocument::create([
            'worker_id' => $w1->id,
            'type' => 'passport',
            'number' => 'P111111',
            'file_path' => 'passports/test1.jpg',
        ]);

        $w2 = Worker::create([
            'name' => 'Worker Philippines Abu Dhabi',
            'email' => 'worker2@example.com',
            'phone' => '+971502222222',
            'password' => bcrypt('password'),
            'nationality' => 'Philippines',
            'category_id' => $this->category->id,
            'verified' => true,
            'status' => 'active',
            'preferred_location' => 'Abu Dhabi',
            'preferred_job_type' => 'part-time',
            'live_in_out' => 'live_out',
            'in_country' => true,
            'visa_status' => 'Tourist Visa',
            'age' => 28,
            'salary' => 1800,
            'experience' => '2 Years',
            'religion' => 'Christian',
            'language' => 'TL',
            'availability' => 'Immediate',
            'bio' => 'Ready to work',
        ]);
        WorkerDocument::create([
            'worker_id' => $w2->id,
            'type' => 'passport',
            'number' => 'P222222',
            'file_path' => 'passports/test2.jpg',
        ]);
    }

    /**
     * Test web workers index page loads correctly.
     */
    public function test_employer_can_view_workers_index()
    {
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers'));

        $response->assertStatus(200);
        $response->assertSee('initialWorkers');
    }

    /**
     * Test web workers index filtering on server-side.
     */
    public function test_employer_workers_web_filtering()
    {
        // 1. Filter by location
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers', ['preferred_location' => 'Abu Dhabi']));
        $response->assertStatus(200);
        $response->assertSee('Worker Philippines Abu Dhabi');
        $response->assertDontSee('Worker India Dubai');

        // 2. Filter by nationality
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers', ['nationality' => 'India']));
        $response->assertStatus(200);
        $response->assertSee('Worker India Dubai');
        $response->assertDontSee('Worker Philippines Abu Dhabi');

        // 3. Filter by job_type
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers', ['job_type' => 'part-time']));
        $response->assertStatus(200);
        $response->assertSee('Worker Philippines Abu Dhabi');
        $response->assertDontSee('Worker India Dubai');

        // 4. Filter by live_in_out
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers', ['live_in_out' => 'live_in']));
        $response->assertStatus(200);
        $response->assertSee('Worker India Dubai');
        $response->assertDontSee('Worker Philippines Abu Dhabi');

        // 5. Filter by visa_status
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.workers', ['visa_status' => 'Tourist Visa']));
        $response->assertStatus(200);
        $response->assertSee('Worker Philippines Abu Dhabi');
        $response->assertDontSee('Worker India Dubai');
    }

    /**
     * Test web candidates index page loads correctly.
     */
    public function test_employer_can_view_candidates_index_and_apply_filters()
    {
        // Add a hired worker (candidate)
        $w = Worker::create([
            'name' => 'Worker Kenya Hired',
            'email' => 'worker3@example.com',
            'phone' => '+971503333333',
            'password' => bcrypt('password'),
            'nationality' => 'Kenya',
            'category_id' => $this->category->id,
            'verified' => true,
            'status' => 'Hired',
            'preferred_location' => 'Dubai',
            'preferred_job_type' => 'full-time',
            'live_in_out' => 'live_out',
            'in_country' => false,
            'visa_status' => 'Cancelled Visa',
            'age' => 30,
            'salary' => 1200,
            'experience' => 'None',
            'religion' => 'Christian',
            'language' => 'SW',
            'availability' => 'Immediate',
            'bio' => 'New helper',
        ]);

        JobOffer::create([
            'employer_id' => $this->employer->id,
            'worker_id' => $w->id,
            'work_date' => now()->format('Y-m-d'),
            'location' => 'Dubai',
            'salary' => 1200,
            'notes' => 'Hired',
            'status' => 'accepted', // Hired status
        ]);

        // Request with location Dubai
        $response = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.candidates', ['preferred_location' => 'Dubai']));

        $response->assertStatus(200);
        $response->assertSee('selectedWorkers');

        // Request with out_country=true
        $response2 = $this->actingAs($this->employer)
            ->withSession(['user' => $this->employer])
            ->get(route('employer.candidates', ['out_country' => 'true']));

        $response2->assertStatus(200);
    }
}
