<?php

namespace Tests\Feature;

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

class EmployerWorkerFilterApiTest extends TestCase
{
    use RefreshDatabase;

    protected $employer;
    protected $token;
    protected $category;

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

        // Create employer
        $this->token = 'test-employer-token';
        $this->employer = User::create([
            'name' => 'Test Employer',
            'email' => 'employer@example.com',
            'password' => bcrypt('Password@123'),
            'role' => 'employer',
            'api_token' => $this->token,
        ]);

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

        // Seed 3 workers with specific filter attributes
        // Worker 1: Dubai, full-time, live-in, India, in_country=true, Residence Visa
        $w1 = Worker::create([
            'name' => 'Worker India In 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',
        ]);

        // Worker 2: Abu Dhabi, part-time, live-out, Philippines, in_country=true, Tourist Visa
        $w2 = Worker::create([
            'name' => 'Worker Philippines In 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',
        ]);

        // Worker 3: Dubai, full-time, live-out, Kenya, in_country=false, Cancelled Visa
        $w3 = Worker::create([
            'name' => 'Worker Kenya Out Country',
            'email' => 'worker3@example.com',
            'phone' => '+971503333333',
            'password' => bcrypt('password'),
            'nationality' => 'Kenya',
            'category_id' => $this->category->id,
            'verified' => true,
            'status' => 'active',
            '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',
        ]);
        WorkerDocument::create([
            'worker_id' => $w3->id,
            'type' => 'passport',
            'number' => 'P333333',
            'file_path' => 'passports/test3.jpg',
        ]);
    }

    /**
     * Test getWorkers filtering.
     */
    public function test_get_workers_preferred_location_filter()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?preferred_location=Abu Dhabi');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(1, $workers);
        $this->assertEquals('Worker Philippines In Abu Dhabi', $workers[0]['name']);
    }

    public function test_get_workers_job_type_filter()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?job_type=part-time');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(1, $workers);
        $this->assertEquals('Worker Philippines In Abu Dhabi', $workers[0]['name']);
    }

    public function test_get_workers_live_in_out_filter()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?live_in_out=live_out');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(2, $workers);
    }

    public function test_get_workers_nationality_multiple_filter()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?nationality=India,Kenya');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(2, $workers);
    }

    public function test_get_workers_in_country_filter()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?in_country=false');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(1, $workers);
        $this->assertEquals('Worker Kenya Out Country', $workers[0]['name']);
    }

    public function test_get_workers_visa_status_if_in_country_filter()
    {
        // Tourist Visa matches Worker 2 (in country)
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?visa_status=Tourist Visa');

        $response->assertStatus(200);
        $workers = $response->json('data.workers');
        $this->assertCount(1, $workers);
        $this->assertEquals('Worker Philippines In Abu Dhabi', $workers[0]['name']);

        // Cancelled Visa is for Worker 3, but Worker 3 is out country, so it should not match
        // because of the "if in country next visa type" constraint.
        $response2 = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/workers?visa_status=Cancelled Visa');

        $response2->assertStatus(200);
        $workers2 = $response2->json('data.workers');
        $this->assertCount(0, $workers2);
    }

    /**
     * Test getCandidates filtering.
     */
    public function test_get_candidates_filters()
    {
        // To be in getCandidates, candidates need status 'Hired' per current logic:
        // Let's create three direct offers with status accepted (which maps to 'Hired')
        $worker1 = Worker::where('nationality', 'India')->first();
        $worker2 = Worker::where('nationality', 'Philippines')->first();
        $worker3 = Worker::where('nationality', 'Kenya')->first();

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

        JobOffer::create([
            'employer_id' => $this->employer->id,
            'worker_id' => $worker2->id,
            'work_date' => now()->format('Y-m-d'),
            'location' => 'Abu Dhabi',
            'salary' => 1500,
            'notes' => 'Hired test 2',
            'status' => 'accepted', // maps to Hired
        ]);

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

        // Filter candidates by preferred location Dubai
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?preferred_location=Dubai');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(2, $candidates);

        // Filter candidates by job_type
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?job_type=part-time');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(1, $candidates);
        $this->assertEquals('Worker Philippines In Abu Dhabi', $candidates[0]['name']);

        // Filter candidates by live_in_out
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?live_in_out=live_in');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(1, $candidates);
        $this->assertEquals('Worker India In Dubai', $candidates[0]['name']);

        // Filter candidates by nationality
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?nationality=Kenya');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(1, $candidates);
        $this->assertEquals('Worker Kenya Out Country', $candidates[0]['name']);

        // Filter candidates by in_country
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?in_country=false');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(1, $candidates);
        $this->assertEquals('Worker Kenya Out Country', $candidates[0]['name']);

        // Filter candidates by visa_status (if in country)
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/candidates?visa_status=Tourist Visa');

        $response->assertStatus(200);
        $candidates = $response->json('data.candidates');
        $this->assertCount(1, $candidates);
        $this->assertEquals('Worker Philippines In Abu Dhabi', $candidates[0]['name']);
    }
}
