<?php

use App\Enums\UserRole;
use App\Filament\Resources\Hunts\Pages\EditHunts;
use App\Filament\Resources\Hunts\RelationManagers\ParticipantsRelationManager;
use App\Models\Hunts;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('detaches a participant from a hunt without deleting the user', function () {
    $admin = User::factory()->create([
        'role' => UserRole::ADMIN,
    ]);

    $hunt = Hunts::withoutSyncingToSearch(fn () => Hunts::factory()->create([
        'creator_id' => $admin->id,
    ]));

    $participant = User::factory()->create();

    $hunt->participants()->attach($participant->id);

    $this->actingAs($admin);

    Livewire::test(ParticipantsRelationManager::class, [
        'ownerRecord' => $hunt,
        'pageClass' => EditHunts::class,
    ])
        ->assertTableActionExists('detach', record: $participant->getKey())
        ->assertTableActionDoesNotExist('delete', record: $participant->getKey())
        ->callTableAction('detach', $participant->getKey());

    $this->assertDatabaseHas('users', [
        'id' => $participant->id,
    ]);

    $this->assertDatabaseMissing('hunt_participants', [
        'hunt_id' => $hunt->id,
        'user_id' => $participant->id,
    ]);
});
