<?php

use App\Enums\TimelineStatus;
use App\Mail\TimelineUpdateEmail;
use App\Models\Timeline;
use App\Models\TimelineUpdate;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;

test('email is queued when timeline update is created', function () {
    Mail::fake();

    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create(['status' => TimelineStatus::Active]);

    TimelineUpdate::factory()->for($timeline)->create();

    Mail::assertQueued(TimelineUpdateEmail::class);
});

test('email is not sent for archived timeline updates', function () {
    Mail::fake();

    $client = User::factory()->create();
    $timeline = Timeline::factory()->for($client)->create(['status' => TimelineStatus::Archived]);

    TimelineUpdate::factory()->for($timeline)->create();

    Mail::assertNothingQueued();
});

test('email uses arabic template when client prefers arabic', function () {
    $client = User::factory()->create(['preferred_language' => 'ar']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->locale)->toBe('ar');
    expect($mailable->content()->markdown)->toBe('emails.timeline.update.ar');
});

test('email uses english template when client prefers english', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->locale)->toBe('en');
    expect($mailable->content()->markdown)->toBe('emails.timeline.update.en');
});

test('email defaults to arabic when default language preference', function () {
    // Database defaults preferred_language to 'ar' when not explicitly set
    $client = User::factory()->create(['preferred_language' => 'ar']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->locale)->toBe('ar');
    expect($mailable->content()->markdown)->toBe('emails.timeline.update.ar');
});

test('email contains case name in subject', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create(['case_name' => 'Smith vs Jones']);
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->envelope()->subject)->toContain('Smith vs Jones');
});

test('email has correct arabic subject', function () {
    $client = User::factory()->create(['preferred_language' => 'ar']);
    $timeline = Timeline::factory()->for($client)->create(['case_name' => 'Test Case']);
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->envelope()->subject)->toBe('تحديث على قضيتك: Test Case');
});

test('email has correct english subject', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create(['case_name' => 'Test Case']);
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);

    expect($mailable->envelope()->subject)->toBe('Update on your case: Test Case');
});

test('email contains update content', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create([
        'update_text' => 'Court date scheduled for next month.',
    ]);

    $mailable = new TimelineUpdateEmail($update);
    $rendered = $mailable->render();

    expect($rendered)->toContain('Court date scheduled for next month.');
});

test('email contains view timeline link', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);
    $rendered = $mailable->render();

    expect($rendered)->toContain(route('client.timelines.show', $timeline));
});

test('email has correct recipient when queued via observer', function () {
    Mail::fake();

    $client = User::factory()->create(['preferred_language' => 'en', 'email' => 'client@example.com']);
    $timeline = Timeline::factory()->for($client)->create(['status' => TimelineStatus::Active]);

    TimelineUpdate::factory()->for($timeline)->create();

    Mail::assertQueued(TimelineUpdateEmail::class, function ($mail) use ($client) {
        return $mail->hasTo($client->email);
    });
});

test('timeline update email implements ShouldQueue', function () {
    expect(TimelineUpdateEmail::class)->toImplement(ShouldQueue::class);
});

test('email renders without errors in Arabic', function () {
    $client = User::factory()->create(['preferred_language' => 'ar']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);
    $rendered = $mailable->render();

    expect($rendered)->toContain('تحديث جديد على قضيتك');
});

test('email renders without errors in English', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);
    $rendered = $mailable->render();

    expect($rendered)->toContain('New Update on Your Case');
});

test('email includes case reference when present', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create([
        'case_name' => 'Test Case',
        'case_reference' => 'REF-2024-001',
    ]);
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);
    $rendered = $mailable->render();

    expect($rendered)->toContain('REF-2024-001');
});

test('email content includes required data', function () {
    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create();
    $update = TimelineUpdate::factory()->for($timeline)->create();

    $mailable = new TimelineUpdateEmail($update);
    $content = $mailable->content();

    expect($content->with)
        ->toHaveKey('update')
        ->toHaveKey('timeline')
        ->toHaveKey('user');
});

test('multiple updates to active timeline send multiple emails', function () {
    Mail::fake();

    $client = User::factory()->create(['preferred_language' => 'en']);
    $timeline = Timeline::factory()->for($client)->create(['status' => TimelineStatus::Active]);

    TimelineUpdate::factory()->for($timeline)->create();
    TimelineUpdate::factory()->for($timeline)->create();
    TimelineUpdate::factory()->for($timeline)->create();

    Mail::assertQueued(TimelineUpdateEmail::class, 3);
});
