<?php

use Modules\Booking\Models\Booking;
use Modules\Booking\Services\BookingRefGenerator;

test('the first booking ref starts the sequence at AAAAA1', function () {
    $ref = (new BookingRefGenerator)->generate();

    expect($ref)->toBe('EVB-AAAAA1');
});

test('the ref increments digit by digit through the alphabet', function () {
    Booking::factory()->create(['booking_ref' => 'EVB-AAAAA9']);

    expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAAAA');
});

test('the ref carries over into the next position once the alphabet is exhausted', function () {
    Booking::factory()->create(['booking_ref' => 'EVB-AAAAZZ']);

    expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAB11');
});

test('generated refs are unique across repeated calls', function () {
    $refs = [];

    for ($i = 0; $i < 20; $i++) {
        $ref = (new BookingRefGenerator)->generate();
        Booking::factory()->create(['booking_ref' => $ref]);
        $refs[] = $ref;
    }

    expect($refs)->toEqual(array_unique($refs));
});

test('an unrecognised existing ref format resets the sequence rather than throwing', function () {
    Booking::factory()->create(['booking_ref' => 'LEGACY-2024-0001']);

    expect((new BookingRefGenerator)->generate())->toBe('EVB-AAAAA1');
});
