<?php

use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Events\DriverAssigned;
use Modules\Booking\Listeners\SendDriverAssignedSms;
use Modules\Booking\Models\Booking;
use Modules\Catalog\Models\Destination;
use Modules\Routing\Models\EvRoute;
use Modules\Shared\Sms\SmsService;

beforeEach(function () {
    config([
        'app.name' => 'FamousLY4 EV',
        'app.support_phone' => '+959123456789',
        'app.support_email' => 'support@famousLY4.test',
    ]);
});

test('a first driver assignment texts the passenger with an "assigned" message including the route', function () {
    $booking = Booking::factory()->create([
        'status' => BookingStatus::Confirmed,
        'passenger_phone' => '+959999888777',
        'driver_name' => 'U Aung',
        'driver_phone' => '+959111222333',
        'car_plate_number' => 'YGN-1234',
        'car_model' => 'Tesla Model Y',
        'ev_route_id' => EvRoute::factory()->create([
            'from_destination_id' => Destination::factory()->create(['name' => 'Yangon'])->id,
            'to_destination_id' => Destination::factory()->create(['name' => 'Mandalay'])->id,
        ])->id,
    ]);

    $sms = Mockery::mock(SmsService::class);
    $sms->shouldReceive('send')
        ->once()
        ->with('+959999888777', Mockery::on(fn (string $message) => str_contains($message, 'assigned')
            && str_contains($message, 'U Aung')
            && str_contains($message, 'YGN-1234')
            && str_contains($message, 'Yangon - Mandalay')
            && str_contains($message, config('app.name'))
            && str_contains($message, config('app.support_phone'))
            && str_contains($message, config('app.support_email'))
            && str_contains($message, 'ယာဉ်မောင်း')
            && str_contains($message, 'အကူအညီလိုအပ်ပါက ဆက်သွယ်ရန်')));

    (new SendDriverAssignedSms($sms))->handle(new DriverAssigned($booking, isFirstAssignment: true));
});

test('a driver reassignment texts the passenger with an "updated" message including the route', function () {
    $booking = Booking::factory()->create([
        'status' => BookingStatus::Confirmed,
        'passenger_phone' => '+959999888777',
        'driver_name' => 'Daw Hla',
        'driver_phone' => '+959444555666',
        'car_plate_number' => 'YGN-5678',
        'ev_route_id' => EvRoute::factory()->create([
            'from_destination_id' => Destination::factory()->create(['name' => 'Yangon'])->id,
            'to_destination_id' => Destination::factory()->create(['name' => 'Mandalay'])->id,
        ])->id,
    ]);

    $sms = Mockery::mock(SmsService::class);
    $sms->shouldReceive('send')
        ->once()
        ->with('+959999888777', Mockery::on(fn (string $message) => str_contains($message, 'updated')
            && str_contains($message, 'Daw Hla')
            && str_contains($message, 'Yangon - Mandalay')
            && str_contains($message, config('app.name'))
            && str_contains($message, config('app.support_phone'))
            && str_contains($message, config('app.support_email'))
            && str_contains($message, 'ယာဉ်မောင်း')
            && str_contains($message, 'အကူအညီလိုအပ်ပါက ဆက်သွယ်ရန်')));

    (new SendDriverAssignedSms($sms))->handle(new DriverAssigned($booking, isFirstAssignment: false));
});
