<?php

use App\Jobs\TrackAnalytics;
use App\Livewire\Mailbox;
use App\Models\Domain;
use App\Models\Email;
use App\Models\Mailbox as MailboxModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;

uses(RefreshDatabase::class);

test('pagination handles no active mailbox gracefully', function () {
    // This tests the fix for "Illuminate\Support\Collection::onFirstPage does not exist"
    Livewire::test(Mailbox::class)
        ->assertStatus(200)
        ->assertViewHas('emails', function ($emails) {
            return $emails instanceof \Illuminate\Contracts\Pagination\LengthAwarePaginator;
        });
});

test('mailbox component renders successfully with active mailbox', function () {
    $domain = Domain::factory()->create(['name' => 'imail.app']);
    $mailbox = MailboxModel::factory()->create([
        'session_id' => session()->getId(),
        'domain_hash' => $domain->domain_hash,
        'address' => 'test@imail.app',
    ]);

    Livewire::test(Mailbox::class)
        ->assertStatus(200)
        ->assertSee('test@imail.app');
});

test('switching mailbox updates session and tracks analytics', function () {
    Queue::fake();

    $domain = Domain::factory()->create();
    $mailbox1 = MailboxModel::factory()->create(['session_id' => session()->getId()]);
    $mailbox2 = MailboxModel::factory()->create(['session_id' => session()->getId()]);

    Livewire::test(Mailbox::class)
        ->set('currentMailboxId', $mailbox1->id)
        ->call('switchMailbox', $mailbox2->id)
        ->assertSet('currentMailboxId', $mailbox2->id);

    expect(session('current_mailbox_id'))->toBe($mailbox2->id);

    Queue::assertPushed(TrackAnalytics::class, function ($job) use ($mailbox2) {
        return $job->eventType === 'mailbox_accessed' && $job->mailboxHash === $mailbox2->mailbox_hash;
    });
});

test('selecting email updates read status and tracks analytics', function () {
    Queue::fake();

    $mailbox = MailboxModel::factory()->create(['session_id' => session()->getId()]);
    $email = Email::factory()->create([
        'recipient_email' => $mailbox->address,
        'is_read' => false,
    ]);

    Livewire::test(Mailbox::class)
        ->set('currentMailboxId', $mailbox->id)
        ->call('selectEmail', $email->id)
        ->assertSet('selectedEmailId', $email->id);

    expect($email->fresh()->is_read)->toBeTrue();

    Queue::assertPushed(TrackAnalytics::class, function ($job) use ($mailbox) {
        return $job->eventType === 'email_read' && $job->mailboxHash === $mailbox->mailbox_hash;
    });
});

test('deleting mailbox performs soft delete and clears session', function () {
    $mailbox = MailboxModel::factory()->create(['session_id' => session()->getId()]);
    session(['current_mailbox_id' => $mailbox->id]);

    Livewire::test(Mailbox::class)
        ->call('deleteMailbox', $mailbox->id);

    $this->assertSoftDeleted('mailboxes', ['id' => $mailbox->id]);
    expect(session('current_mailbox_id'))->toBeNull();
});
