<?php

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

test('admin email has action required prefix in English subject', function () {
    User::factory()->admin()->create(['preferred_language' => 'en']);
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $mailable = new NewBookingAdminEmail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('[Action Required] New Consultation Request');
});

test('admin email has action required prefix in Arabic subject', function () {
    User::factory()->admin()->create(['preferred_language' => 'ar']);
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $mailable = new NewBookingAdminEmail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('[إجراء مطلوب] طلب استشارة جديد');
});

test('admin email defaults to English when admin has no language preference', function () {
    User::factory()->admin()->create(['preferred_language' => 'en']);
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $mailable = new NewBookingAdminEmail($consultation);

    expect($mailable->envelope()->subject)
        ->toContain('[Action Required]');
});

test('admin email includes full problem summary without truncation', function () {
    User::factory()->admin()->create();
    $client = User::factory()->individual()->create();
    $longSummary = str_repeat('Legal issue description. ', 50);
    $consultation = Consultation::factory()->create([
        'user_id' => $client->id,
        'problem_summary' => $longSummary,
    ]);

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

    expect($content->with['consultation']->problem_summary)
        ->toBe($longSummary);
});

test('admin email includes review URL', function () {
    User::factory()->admin()->create();
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

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

    expect($content->with['reviewUrl'])
        ->toContain('consultations')
        ->toContain((string) $consultation->id);
});

test('admin email is sent when consultation is created', function () {
    Mail::fake();

    $admin = User::factory()->admin()->create();
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    Mail::to($admin->email)->queue(new NewBookingAdminEmail($consultation));

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

test('admin email is queued for async delivery', function () {
    Mail::fake();

    $admin = User::factory()->admin()->create();
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    Mail::to($admin->email)->queue(new NewBookingAdminEmail($consultation));

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

test('warning is logged when no admin exists', function () {
    Log::shouldReceive('warning')
        ->once()
        ->with('No admin user found to notify about new booking', \Mockery::type('array'));

    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $admin = User::query()->where('user_type', 'admin')->first();

    if (! $admin) {
        Log::warning('No admin user found to notify about new booking', [
            'consultation_id' => $consultation->id,
        ]);
    }
});

test('admin email displays company client information correctly', function () {
    User::factory()->admin()->create();
    $companyClient = User::factory()->company()->create([
        'company_name' => 'Acme Corp',
        'contact_person_name' => 'John Doe',
    ]);
    $consultation = Consultation::factory()->create(['user_id' => $companyClient->id]);

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

    expect($content->with['client']->company_name)->toBe('Acme Corp');
    expect($content->with['client']->contact_person_name)->toBe('John Doe');
});

test('admin email displays individual client information correctly', function () {
    User::factory()->admin()->create();
    $individualClient = User::factory()->individual()->create([
        'full_name' => 'Jane Smith',
    ]);
    $consultation = Consultation::factory()->create(['user_id' => $individualClient->id]);

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

    expect($content->with['client']->full_name)->toBe('Jane Smith');
    expect($content->with['client']->user_type->value)->toBe('individual');
});

test('admin email implements ShouldQueue', function () {
    expect(NewBookingAdminEmail::class)
        ->toImplement(ShouldQueue::class);
});

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

    $mailable = new NewBookingAdminEmail($consultation);

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

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

    $mailable = new NewBookingAdminEmail($consultation);

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

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

    $mailable = new NewBookingAdminEmail($consultation);

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

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

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

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

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

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

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

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

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

    expect($content->with)
        ->toHaveKey('consultation')
        ->toHaveKey('client')
        ->toHaveKey('formattedDate')
        ->toHaveKey('formattedTime')
        ->toHaveKey('reviewUrl');
});

test('admin email includes client email and phone', function () {
    User::factory()->admin()->create();
    $client = User::factory()->individual()->create([
        'email' => 'client@example.com',
        'phone' => '+1234567890',
    ]);
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

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

    expect($content->with['client']->email)->toBe('client@example.com');
    expect($content->with['client']->phone)->toBe('+1234567890');
});

test('review URL points to admin consultation show page', function () {
    User::factory()->admin()->create();
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $mailable = new NewBookingAdminEmail($consultation);

    expect($mailable->getReviewUrl())
        ->toBe(route('admin.consultations.show', $consultation));
});

test('defaults to English when no admin exists', function () {
    $client = User::factory()->individual()->create();
    $consultation = Consultation::factory()->create(['user_id' => $client->id]);

    $mailable = new NewBookingAdminEmail($consultation);

    expect($mailable->envelope()->subject)
        ->toBe('[Action Required] New Consultation Request');
});
