<?php

declare(strict_types=1);

use App\Enums\Auth\CrudPermission;
use App\Models\Auth\User;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use Laravel\Sanctum\Sanctum;

use function Pest\Laravel\put;

test('protected', function () {
    $entry = AnimeThemeEntry::factory()
        ->for(AnimeTheme::factory()->for(Anime::factory()))
        ->createOne();

    $parameters = AnimeThemeEntry::factory()->raw();

    $response = put(route('api.animethemeentry.update', ['animethemeentry' => $entry] + $parameters));

    $response->assertUnauthorized();
});

test('forbidden', function () {
    $entry = AnimeThemeEntry::factory()
        ->for(AnimeTheme::factory()->for(Anime::factory()))
        ->createOne();

    $parameters = AnimeThemeEntry::factory()->raw();

    $user = User::factory()->createOne();

    Sanctum::actingAs($user);

    $response = put(route('api.animethemeentry.update', ['animethemeentry' => $entry] + $parameters));

    $response->assertForbidden();
});

test('trashed', function () {
    $entry = AnimeThemeEntry::factory()
        ->trashed()
        ->for(AnimeTheme::factory()->for(Anime::factory()))
        ->createOne();

    $parameters = AnimeThemeEntry::factory()->raw();

    $user = User::factory()->withPermissions(CrudPermission::UPDATE->format(AnimeThemeEntry::class))->createOne();

    Sanctum::actingAs($user);

    $response = put(route('api.animethemeentry.update', ['animethemeentry' => $entry] + $parameters));

    $response->assertNotFound();
});

test('update', function () {
    $entry = AnimeThemeEntry::factory()
        ->for(AnimeTheme::factory()->for(Anime::factory()))
        ->createOne();

    $parameters = AnimeThemeEntry::factory()->raw();

    $user = User::factory()->withPermissions(CrudPermission::UPDATE->format(AnimeThemeEntry::class))->createOne();

    Sanctum::actingAs($user);

    $response = put(route('api.animethemeentry.update', ['animethemeentry' => $entry] + $parameters));

    $response->assertOk();
});
