<?php

declare(strict_types=1);

use App\Enums\Auth\CrudPermission;
use App\Enums\Auth\SpecialPermission;
use App\Filament\Actions\Base\CreateAction;
use App\Filament\Actions\Base\DeleteAction;
use App\Filament\Actions\Base\EditAction;
use App\Filament\Actions\Base\ForceDeleteAction;
use App\Filament\Actions\Base\RestoreAction;
use App\Filament\Resources\Wiki\Song\PerformanceResource;
use App\Models\Auth\User;
use App\Models\Wiki\Artist;
use App\Models\Wiki\Song;
use App\Models\Wiki\Song\Performance as PerformanceModel;
use Filament\Actions\Testing\TestAction;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;

use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;

test('render index page', function (): void {
    $user = User::factory()
        ->withPermissions(
            SpecialPermission::VIEW_FILAMENT->value,
            CrudPermission::VIEW->format(PerformanceModel::class)
        )
        ->createOne();

    actingAs($user);

    $records = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->create();

    get(PerformanceResource::getUrl('index'))
        ->assertSuccessful();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->assertCanSeeTableRecords(collect([$records]));
});

test('render view page', function (): void {
    $user = User::factory()
        ->withPermissions(
            SpecialPermission::VIEW_FILAMENT->value,
            CrudPermission::VIEW->format(PerformanceModel::class)
        )
        ->createOne();

    actingAs($user);

    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    get(PerformanceResource::getUrl('view', ['record' => $record]))
        ->assertSuccessful();
});

test('mount create action', function (): void {
    $user = User::factory()
        ->withPermissions(
            SpecialPermission::VIEW_FILAMENT->value,
            CrudPermission::CREATE->format(PerformanceModel::class)
        )
        ->createOne();

    actingAs($user);

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->mountAction(CreateAction::class)
        ->assertActionMounted(CreateAction::class);
});

test('mount edit action', function (): void {
    if (DB::getDriverName() === 'sqlite') {
        $this->markTestSkipped('SQLite does not support this test properly.');
    }

    $user = User::factory()
        ->withPermissions(
            SpecialPermission::VIEW_FILAMENT->value,
            CrudPermission::UPDATE->format(PerformanceModel::class)
        )
        ->createOne();

    actingAs($user);

    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->mountAction(TestAction::make(EditAction::getDefaultName())->table($record))
        ->callMountedAction()
        ->assertHasNoErrors();
});

test('user cannot create record', function (): void {
    Livewire::test(getIndexPage(PerformanceResource::class))
        ->assertActionHidden(CreateAction::class);
});

test('user cannot edit record', function (): void {
    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->assertActionDoesNotExist(TestAction::make(EditAction::getDefaultName())->table($record));
});

test('user cannot delete record', function (): void {
    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->assertActionDoesNotExist(TestAction::make(DeleteAction::getDefaultName())->table($record));
});

test('user cannot restore record', function (): void {
    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    $record->delete();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->filterTable('trashed', 0)
        ->assertActionDoesNotExist(TestAction::make(RestoreAction::getDefaultName())->table($record));
});

test('user cannot force delete record', function (): void {
    $record = PerformanceModel::factory()
        ->for(Song::factory())
        ->for(Artist::factory(), PerformanceModel::RELATION_ARTIST)
        ->createOne();

    Livewire::test(getIndexPage(PerformanceResource::class))
        ->assertActionDoesNotExist(TestAction::make(ForceDeleteAction::getDefaultName())->table($record));
});
