<?php

declare(strict_types=1);

use App\Enums\Auth\CrudPermission;
use App\Models\Auth\User;
use App\Models\Wiki\Audio;
use Laravel\Sanctum\Sanctum;

use function Pest\Laravel\post;

test('protected', function (): void {
    $audio = Audio::factory()->makeOne();

    $response = post(route('api.audio.store', $audio->toArray()));

    $response->assertUnauthorized();
});

test('forbidden', function (): void {
    $audio = Audio::factory()->makeOne();

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

    Sanctum::actingAs($user);

    $response = post(route('api.audio.store', $audio->toArray()));

    $response->assertForbidden();
});

test('required fields', function (): void {
    $user = User::factory()->withPermissions(CrudPermission::CREATE->format(Audio::class))->createOne();

    Sanctum::actingAs($user);

    $response = post(route('api.audio.store'));

    $response->assertJsonValidationErrors([
        Audio::ATTRIBUTE_BASENAME,
        Audio::ATTRIBUTE_FILENAME,
        Audio::ATTRIBUTE_MIMETYPE,
        Audio::ATTRIBUTE_PATH,
        Audio::ATTRIBUTE_SIZE,
    ]);
});

test('create', function (): void {
    $parameters = Audio::factory()->raw();

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

    Sanctum::actingAs($user);

    $response = post(route('api.audio.store', $parameters));

    $response->assertCreated();
    $this->assertDatabaseCount(Audio::class, 1);
});
