<?php

use App\Models\User;
use Livewire\Livewire;
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\CreateDepartureTimeSlot;
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\EditDepartureTimeSlot;
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\ListDepartureTimeSlots;
use Modules\Catalog\Models\DepartureTimeSlot;
use Spatie\Permission\Models\Role;

use function Pest\Laravel\assertDatabaseHas;

beforeEach(function () {
    Role::findOrCreate('admin', 'web');

    $this->admin = User::factory()->create()->assignRole('admin');
    $this->actingAs($this->admin);
});

test('can list departure time slots', function () {
    $timeSlots = DepartureTimeSlot::factory()->count(3)->create();

    Livewire::test(ListDepartureTimeSlots::class)
        ->assertOk()
        ->assertCanSeeTableRecords($timeSlots);
});

test('can create a departure time slot', function () {
    $timeSlot = DepartureTimeSlot::factory()->make();

    Livewire::test(CreateDepartureTimeSlot::class)
        ->fillForm([
            'label' => $timeSlot->label,
            'time' => $timeSlot->time,
            'is_active' => true,
        ])
        ->call('create')
        ->assertNotified()
        ->assertRedirect();

    assertDatabaseHas(DepartureTimeSlot::class, [
        'label' => $timeSlot->label,
    ]);
});

test('can edit a departure time slot', function () {
    $timeSlot = DepartureTimeSlot::factory()->create();

    Livewire::test(EditDepartureTimeSlot::class, ['record' => $timeSlot->getRouteKey()])
        ->assertOk()
        ->fillForm(['label' => 'Updated Label'])
        ->call('save')
        ->assertNotified();

    assertDatabaseHas(DepartureTimeSlot::class, [
        'id' => $timeSlot->id,
        'label' => 'Updated Label',
    ]);
});

test('a departure time slot is a shared catalog entity not owned by a single route', function () {
    $timeSlot = DepartureTimeSlot::factory()->create();

    expect($timeSlot->getFillable())->not->toContain('ev_route_id');
});
