<?php

use App\Mail\BaseMailable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;

class TestMailable extends BaseMailable
{
    public function content(): Content
    {
        return new Content(
            markdown: 'mail.test',
        );
    }
}

test('mail configuration loads correctly', function () {
    expect(config('mail.from.address'))->toBe('no-reply@libra.ps');
    expect(config('mail.from.name'))->toBe('Libra Law Firm');
});

test('base mailable implements should queue interface', function () {
    $mailable = new TestMailable;

    expect($mailable)->toBeInstanceOf(ShouldQueue::class);
});

test('sender name is arabic when locale is ar', function () {
    app()->setLocale('ar');

    $mailable = new TestMailable;

    expect($mailable->getFromName())->toBe('مكتب ليبرا للمحاماة');
});

test('sender name is english when locale is en', function () {
    app()->setLocale('en');

    $mailable = new TestMailable;

    expect($mailable->getFromName())->toBe('Libra Law Firm');
});

test('sender name respects mailable locale over app locale', function () {
    app()->setLocale('en');

    $mailable = new TestMailable;
    $mailable->locale('ar');

    expect($mailable->getFromName())->toBe('مكتب ليبرا للمحاماة');
});

test('emails are queued not sent synchronously', function () {
    Queue::fake();

    Mail::to('test@example.com')->queue(new TestMailable);

    Queue::assertPushed(\Illuminate\Mail\SendQueuedMailable::class);
});

test('envelope contains correct from address', function () {
    config(['mail.from.address' => 'no-reply@libra.ps']);

    $mailable = new TestMailable;
    $envelope = $mailable->envelope();

    expect($envelope)->toBeInstanceOf(Envelope::class);
    expect($envelope->from->address)->toBe('no-reply@libra.ps');
});

test('production queue connection is database', function () {
    // In production, queue should use database driver
    // This test verifies the config file default
    $queueConfig = include base_path('config/queue.php');
    expect($queueConfig['default'])->toBe(env('QUEUE_CONNECTION', 'database'));
});
