<?php

use App\Models\PotentialClient;
use App\Models\User;
use Livewire\Volt\Volt;

beforeEach(function () {
    $this->admin = User::factory()->admin()->create();
});

// ===========================================
// Access Tests
// ===========================================

test('admin can access potential clients index page', function () {
    $this->actingAs($this->admin)
        ->get(route('admin.potential-clients.index'))
        ->assertOk();
});

test('non-admin cannot access potential clients index page', function () {
    $client = User::factory()->individual()->create();

    $this->actingAs($client)
        ->get(route('admin.potential-clients.index'))
        ->assertForbidden();
});

test('unauthenticated user cannot access potential clients index page', function () {
    $this->get(route('admin.potential-clients.index'))
        ->assertRedirect(route('login'));
});

// ===========================================
// List Display Tests
// ===========================================

test('index page displays potential clients', function () {
    $potentialClient = PotentialClient::factory()->create(['name' => 'Test Potential']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSee('Test Potential');
});

test('potential clients sorted by created_at desc by default', function () {
    $oldClient = PotentialClient::factory()->create([
        'name' => 'Old Client',
        'created_at' => now()->subDays(10),
    ]);

    $newClient = PotentialClient::factory()->create([
        'name' => 'New Client',
        'created_at' => now(),
    ]);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSeeInOrder(['New Client', 'Old Client']);
});

test('displays type badge for individual potential client', function () {
    PotentialClient::factory()->individual()->create(['name' => 'Individual Test']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSee('Individual Test');
});

test('displays type badge for company potential client', function () {
    PotentialClient::factory()->company()->create(['name' => 'Company Test']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSee('Company Test');
});

test('displays type badge for agency potential client', function () {
    PotentialClient::factory()->agency()->create(['name' => 'Agency Test']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSee('Agency Test');
});

// ===========================================
// Search Tests
// ===========================================

test('can search potential clients by name', function () {
    PotentialClient::factory()->create(['name' => 'John Doe']);
    PotentialClient::factory()->create(['name' => 'Jane Smith']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('search', 'John')
        ->assertSee('John Doe')
        ->assertDontSee('Jane Smith');
});

test('can search potential clients by email', function () {
    PotentialClient::factory()->create([
        'name' => 'John Doe',
        'email' => 'john@example.com',
    ]);
    PotentialClient::factory()->create([
        'name' => 'Jane Smith',
        'email' => 'jane@example.com',
    ]);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('search', 'john@')
        ->assertSee('John Doe')
        ->assertDontSee('Jane Smith');
});

test('can search potential clients by phone', function () {
    PotentialClient::factory()->create([
        'name' => 'John Doe',
        'phone' => '+970599111111',
    ]);
    PotentialClient::factory()->create([
        'name' => 'Jane Smith',
        'phone' => '+970599222222',
    ]);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('search', '111111')
        ->assertSee('John Doe')
        ->assertDontSee('Jane Smith');
});

// ===========================================
// Type Filter Tests
// ===========================================

test('can filter potential clients by individual type', function () {
    PotentialClient::factory()->individual()->create(['name' => 'Individual Client']);
    PotentialClient::factory()->company()->create(['name' => 'Company Client']);
    PotentialClient::factory()->agency()->create(['name' => 'Agency Client']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('typeFilter', 'individual')
        ->assertSee('Individual Client')
        ->assertDontSee('Company Client')
        ->assertDontSee('Agency Client');
});

test('can filter potential clients by company type', function () {
    PotentialClient::factory()->individual()->create(['name' => 'Individual Client']);
    PotentialClient::factory()->company()->create(['name' => 'Company Client']);
    PotentialClient::factory()->agency()->create(['name' => 'Agency Client']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('typeFilter', 'company')
        ->assertDontSee('Individual Client')
        ->assertSee('Company Client')
        ->assertDontSee('Agency Client');
});

test('can filter potential clients by agency type', function () {
    PotentialClient::factory()->individual()->create(['name' => 'Individual Client']);
    PotentialClient::factory()->company()->create(['name' => 'Company Client']);
    PotentialClient::factory()->agency()->create(['name' => 'Agency Client']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('typeFilter', 'agency')
        ->assertDontSee('Individual Client')
        ->assertDontSee('Company Client')
        ->assertSee('Agency Client');
});

// ===========================================
// Clear Filters Tests
// ===========================================

test('clear filters resets search and type filter', function () {
    PotentialClient::factory()->create(['name' => 'Test Client']);

    $this->actingAs($this->admin);

    $component = Volt::test('admin.potential-clients.index')
        ->set('search', 'something')
        ->set('typeFilter', 'individual')
        ->call('clearFilters');

    expect($component->get('search'))->toBe('');
    expect($component->get('typeFilter'))->toBe('');
});

// ===========================================
// Pagination Tests
// ===========================================

test('can change per page value', function () {
    $this->actingAs($this->admin);

    $component = Volt::test('admin.potential-clients.index')
        ->set('perPage', 25);

    expect($component->get('perPage'))->toBe(25);
});

test('changing per page resets to first page', function () {
    PotentialClient::factory()->count(15)->create();

    $this->actingAs($this->admin);

    $component = Volt::test('admin.potential-clients.index')
        ->set('perPage', 10)
        ->call('gotoPage', 2)
        ->set('perPage', 25);

    // After changing perPage, page should reset
    expect($component->get('perPage'))->toBe(25);
});

// ===========================================
// Empty State Tests
// ===========================================

test('shows empty state message when no potential clients exist', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->assertSee(__('potential-clients.no_potential_clients_found'));
});

test('shows filter empty state when no potential clients match filters', function () {
    PotentialClient::factory()->company()->create(['name' => 'Company Only']);

    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.index')
        ->set('typeFilter', 'individual')
        ->assertSee(__('potential-clients.no_potential_clients_match'));
});

// ===========================================
// Search Resets Pagination Tests
// ===========================================

test('updating search resets page to 1', function () {
    PotentialClient::factory()->count(15)->create();

    $this->actingAs($this->admin);

    $component = Volt::test('admin.potential-clients.index')
        ->call('gotoPage', 2)
        ->set('search', 'test');

    // Page should be reset after search update
    expect($component->get('search'))->toBe('test');
});

test('updating type filter resets page to 1', function () {
    PotentialClient::factory()->count(15)->create();

    $this->actingAs($this->admin);

    $component = Volt::test('admin.potential-clients.index')
        ->call('gotoPage', 2)
        ->set('typeFilter', 'individual');

    // Page should be reset after filter update
    expect($component->get('typeFilter'))->toBe('individual');
});
