<?php

use App\Mail\BookingSubmittedMail;
use App\Models\Consultation;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;

test('booking submitted email has correct subject in Arabic', function () {
    $user = User::factory()->create(['preferred_language' => 'ar']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('تم استلام طلب الاستشارة');
});

test('booking submitted email has correct subject in English', function () {
    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('Your Consultation Request Has Been Submitted');
});

test('problem summary is truncated at 200 characters', function () {
    $longSummary = str_repeat('a', 250);
    $user = User::factory()->create();
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'problem_summary' => $longSummary,
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getSummaryPreview())
        ->toHaveLength(203) // 200 + '...'
        ->toEndWith('...');
});

test('date is formatted as d/m/Y for Arabic users', function () {
    $user = User::factory()->create(['preferred_language' => 'ar']);
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'booking_date' => '2025-03-15',
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getFormattedDate('ar'))->toBe('15/03/2025');
});

test('date is formatted as m/d/Y for English users', function () {
    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'booking_date' => '2025-03-15',
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getFormattedDate('en'))->toBe('03/15/2025');
});

test('defaults to Arabic when preferred_language is ar', function () {
    // Note: Database has NOT NULL constraint on preferred_language
    // Default behavior is tested with 'ar' as that's the default value
    $user = User::factory()->create(['preferred_language' => 'ar']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('تم استلام طلب الاستشارة');
});

test('empty problem summary returns empty string', function () {
    $user = User::factory()->create();
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'problem_summary' => '',
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getSummaryPreview())->toBe('');
});

test('summary exactly 200 characters is not truncated', function () {
    // Note: Database has NOT NULL constraint on problem_summary
    // Testing exact boundary: 200 chars should NOT have ellipsis
    $exactSummary = str_repeat('a', 200);
    $user = User::factory()->create();
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'problem_summary' => $exactSummary,
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getSummaryPreview())
        ->toHaveLength(200)
        ->not->toEndWith('...');
});

test('booking submitted email implements ShouldQueue', function () {
    expect(BookingSubmittedMail::class)
        ->toImplement(ShouldQueue::class);
});

test('summary under 200 characters is not truncated', function () {
    $shortSummary = 'This is a short summary.';
    $user = User::factory()->create();
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'problem_summary' => $shortSummary,
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getSummaryPreview())
        ->toBe($shortSummary)
        ->not->toEndWith('...');
});

test('time is formatted as h:i A', function () {
    $user = User::factory()->create();
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'booking_time' => '14:30:00',
    ]);

    $mailable = new BookingSubmittedMail($consultation);

    expect($mailable->getFormattedTime())->toBe('02:30 PM');
});

test('uses correct Arabic template for Arabic users', function () {
    $user = User::factory()->create(['preferred_language' => 'ar']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    $mailable = new BookingSubmittedMail($consultation);
    $content = $mailable->content();

    expect($content->markdown)->toBe('emails.booking.submitted.ar');
});

test('uses correct English template for English users', function () {
    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    $mailable = new BookingSubmittedMail($consultation);
    $content = $mailable->content();

    expect($content->markdown)->toBe('emails.booking.submitted.en');
});

test('content includes all required data', function () {
    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create([
        'user_id' => $user->id,
        'booking_date' => '2025-03-15',
        'booking_time' => '10:00:00',
        'problem_summary' => 'Test summary',
    ]);

    $mailable = new BookingSubmittedMail($consultation);
    $content = $mailable->content();

    expect($content->with)
        ->toHaveKey('consultation')
        ->toHaveKey('user')
        ->toHaveKey('summaryPreview')
        ->toHaveKey('formattedDate')
        ->toHaveKey('formattedTime');
});

test('email is queued when sent', function () {
    Mail::fake();

    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    Mail::to($user->email)->queue(new BookingSubmittedMail($consultation));

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

test('email has correct recipient', function () {
    Mail::fake();

    $user = User::factory()->create(['preferred_language' => 'en']);
    $consultation = Consultation::factory()->create(['user_id' => $user->id]);

    Mail::to($user->email)->queue(new BookingSubmittedMail($consultation));

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