<?php

declare(strict_types=1);

use App\Enums\Auth\CrudPermission;
use App\Models\Admin\Feature;
use App\Models\Auth\User;
use Laravel\Sanctum\Sanctum;

use function Pest\Laravel\put;

test('protected', function (): void {
    $feature = Feature::factory()->createOne();

    $parameters = [
        Feature::ATTRIBUTE_VALUE => ! $feature->value,
    ];

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

    $response->assertUnauthorized();
});

test('forbidden', function (): void {
    $feature = Feature::factory()->createOne();

    $parameters = [
        Feature::ATTRIBUTE_VALUE => ! $feature->value,
    ];

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

    Sanctum::actingAs($user);

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

    $response->assertForbidden();
});

test('update', function (): void {
    $feature = Feature::factory()->createOne();

    $parameters = [
        Feature::ATTRIBUTE_VALUE => ! $feature->value,
    ];

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

    Sanctum::actingAs($user);

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

    $response->assertOk();
});
