<?php

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

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

// ===========================================
// Create Page Access Tests
// ===========================================

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

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

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

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

// ===========================================
// Create Form Submission Tests
// ===========================================

test('admin can create potential client with valid data', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'individual')
        ->set('name', 'Test Potential Client')
        ->set('phone', '+970599123456')
        ->set('email', 'test@example.com')
        ->set('address', '123 Test Street')
        ->set('social_media', '@testhandle')
        ->set('website', 'https://example.com')
        ->set('notes', 'Test notes')
        ->call('create')
        ->assertHasNoErrors()
        ->assertRedirect(route('admin.potential-clients.index'));

    expect(PotentialClient::where('name', 'Test Potential Client')->exists())->toBeTrue();
});

test('admin can create potential client with only required type field', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'company')
        ->call('create')
        ->assertHasNoErrors()
        ->assertRedirect(route('admin.potential-clients.index'));

    expect(PotentialClient::where('type', PotentialClientType::Company)->exists())->toBeTrue();
});

test('type is correctly saved as enum value', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'agency')
        ->set('name', 'Agency Client')
        ->call('create')
        ->assertHasNoErrors();

    $client = PotentialClient::where('name', 'Agency Client')->first();
    expect($client->type)->toBe(PotentialClientType::Agency);
});

// ===========================================
// Create Form Validation Tests
// ===========================================

test('cannot create potential client without type', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', '')
        ->set('name', 'Test Name')
        ->call('create')
        ->assertHasErrors(['type' => 'required']);
});

test('cannot create potential client with invalid type', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'invalid_type')
        ->call('create')
        ->assertHasErrors(['type' => 'in']);
});

test('cannot create potential client with invalid email format', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'individual')
        ->set('email', 'not-an-email')
        ->call('create')
        ->assertHasErrors(['email' => 'email']);
});

test('cannot create potential client with invalid website URL', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'individual')
        ->set('website', 'not-a-url')
        ->call('create')
        ->assertHasErrors(['website' => 'url']);
});

test('name must not exceed 255 characters', function () {
    $this->actingAs($this->admin);

    Volt::test('admin.potential-clients.create')
        ->set('type', 'individual')
        ->set('name', str_repeat('a', 256))
        ->call('create')
        ->assertHasErrors(['name' => 'max']);
});

// ===========================================
// Show Page Access Tests
// ===========================================

test('admin can access view potential client page', function () {
    $potentialClient = PotentialClient::factory()->create();

    $this->actingAs($this->admin)
        ->get(route('admin.potential-clients.show', $potentialClient))
        ->assertOk();
});

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

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

test('unauthenticated user cannot access view potential client page', function () {
    $potentialClient = PotentialClient::factory()->create();

    $this->get(route('admin.potential-clients.show', $potentialClient))
        ->assertRedirect(route('login'));
});

// ===========================================
// Show Page Display Tests
// ===========================================

test('show page displays potential client information', function () {
    $potentialClient = PotentialClient::factory()->create([
        'name' => 'John Doe',
        'email' => 'john@example.com',
        'phone' => '+970599123456',
        'address' => '123 Test Street',
    ]);

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee('John Doe')
        ->assertSee('john@example.com')
        ->assertSee('+970599123456')
        ->assertSee('123 Test Street');
});

test('show page displays type badge correctly', function () {
    $potentialClient = PotentialClient::factory()->individual()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee($potentialClient->type->label());
});

test('show page displays not provided for empty fields', function () {
    $potentialClient = PotentialClient::factory()->create([
        'name' => null,
        'email' => null,
        'phone' => null,
    ]);

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee(__('potential-clients.not_provided'));
});

test('show page displays created and updated dates', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee($potentialClient->created_at->format('Y-m-d H:i'))
        ->assertSee($potentialClient->updated_at->format('Y-m-d H:i'));
});

// ===========================================
// Edit Page Access Tests
// ===========================================

test('admin can access edit potential client page', function () {
    $potentialClient = PotentialClient::factory()->create();

    $this->actingAs($this->admin)
        ->get(route('admin.potential-clients.edit', $potentialClient))
        ->assertOk();
});

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

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

test('unauthenticated user cannot access edit potential client page', function () {
    $potentialClient = PotentialClient::factory()->create();

    $this->get(route('admin.potential-clients.edit', $potentialClient))
        ->assertRedirect(route('login'));
});

// ===========================================
// Edit Form Pre-population Tests
// ===========================================

test('edit form pre-populates with current values', function () {
    $potentialClient = PotentialClient::factory()->create([
        'type' => PotentialClientType::Company,
        'name' => 'Original Name',
        'email' => 'original@example.com',
        'phone' => '+970599000000',
        'address' => 'Original Address',
        'social_media' => '@original',
        'website' => 'https://original.com',
        'notes' => 'Original notes',
    ]);

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

    $component = Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient]);

    expect($component->get('type'))->toBe('company');
    expect($component->get('name'))->toBe('Original Name');
    expect($component->get('email'))->toBe('original@example.com');
    expect($component->get('phone'))->toBe('+970599000000');
    expect($component->get('address'))->toBe('Original Address');
    expect($component->get('social_media'))->toBe('@original');
    expect($component->get('website'))->toBe('https://original.com');
    expect($component->get('notes'))->toBe('Original notes');
});

// ===========================================
// Edit Form Submission Tests
// ===========================================

test('admin can update potential client with valid data', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->set('type', 'agency')
        ->set('name', 'Updated Name')
        ->set('email', 'updated@example.com')
        ->set('phone', '+970599111111')
        ->call('update')
        ->assertHasNoErrors()
        ->assertRedirect(route('admin.potential-clients.show', $potentialClient));

    $potentialClient->refresh();

    expect($potentialClient->type)->toBe(PotentialClientType::Agency);
    expect($potentialClient->name)->toBe('Updated Name');
    expect($potentialClient->email)->toBe('updated@example.com');
    expect($potentialClient->phone)->toBe('+970599111111');
});

test('edit form shows success message on update', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->set('type', 'individual')
        ->call('update')
        ->assertHasNoErrors();

    expect(session('success'))->toBe(__('potential-clients.updated_success'));
});

// ===========================================
// Edit Form Validation Tests
// ===========================================

test('cannot update potential client without type', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->set('type', '')
        ->call('update')
        ->assertHasErrors(['type' => 'required']);
});

test('cannot update potential client with invalid email', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->set('email', 'invalid-email')
        ->call('update')
        ->assertHasErrors(['email' => 'email']);
});

test('cannot update potential client with invalid website URL', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->set('website', 'not-a-url')
        ->call('update')
        ->assertHasErrors(['website' => 'url']);
});

// ===========================================
// Delete from Show Page Tests
// ===========================================

test('admin can delete potential client from show page', function () {
    $potentialClient = PotentialClient::factory()->create(['name' => 'To Be Deleted']);

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->call('confirmDelete')
        ->assertSet('showDeleteModal', true)
        ->call('delete')
        ->assertRedirect(route('admin.potential-clients.index'));

    expect(PotentialClient::where('name', 'To Be Deleted')->exists())->toBeFalse();
});

test('delete confirmation shows potential client name', function () {
    $potentialClient = PotentialClient::factory()->create(['name' => 'Client To Delete']);

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->call('confirmDelete')
        ->assertSet('showDeleteModal', true)
        ->assertSee('Client To Delete');
});

test('can cancel delete confirmation on show page', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->call('confirmDelete')
        ->assertSet('showDeleteModal', true)
        ->call('cancelDelete')
        ->assertSet('showDeleteModal', false);

    // Client should still exist
    expect(PotentialClient::find($potentialClient->id))->not->toBeNull();
});

// ===========================================
// Delete from Index Page Tests
// ===========================================

test('admin can delete potential client from index page', function () {
    $potentialClient = PotentialClient::factory()->create(['name' => 'Index Delete Test']);

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

    Volt::test('admin.potential-clients.index')
        ->call('confirmDelete', $potentialClient->id)
        ->assertSet('showDeleteModal', true)
        ->assertSet('deletingClientId', $potentialClient->id)
        ->call('delete');

    expect(PotentialClient::where('name', 'Index Delete Test')->exists())->toBeFalse();
});

test('can cancel delete confirmation on index page', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.index')
        ->call('confirmDelete', $potentialClient->id)
        ->assertSet('showDeleteModal', true)
        ->call('cancelDelete')
        ->assertSet('showDeleteModal', false)
        ->assertSet('deletingClientId', null);

    // Client should still exist
    expect(PotentialClient::find($potentialClient->id))->not->toBeNull();
});

test('delete removes the potential client from database', function () {
    $potentialClient = PotentialClient::factory()->create();
    $clientId = $potentialClient->id;

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

    Volt::test('admin.potential-clients.index')
        ->call('confirmDelete', $potentialClient->id)
        ->call('delete')
        ->assertSet('showDeleteModal', false)
        ->assertSet('deletingClientId', null);

    expect(PotentialClient::find($clientId))->toBeNull();
});

// ===========================================
// Navigation Tests
// ===========================================

test('create page has back to list link', function () {
    $this->actingAs($this->admin);

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

test('show page has back to list link', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee(__('potential-clients.back_to_list'));
});

test('edit page has back to details link', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.edit', ['potentialClient' => $potentialClient])
        ->assertSee(__('potential-clients.back_to_details'));
});

test('show page has edit button', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee(__('potential-clients.edit'));
});

test('show page has delete button', function () {
    $potentialClient = PotentialClient::factory()->create();

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

    Volt::test('admin.potential-clients.show', ['potentialClient' => $potentialClient])
        ->assertSee(__('potential-clients.delete'));
});
