<?php

namespace Tests\Feature\Seeders;

use App\Domains\Authorization\Enums\PermissionCode;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Authorization\Models\Permission;
use App\Domains\Authorization\Models\Role;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthorizationSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_creates_the_initial_roles_and_permissions(): void
    {
        $this->seed(AuthorizationSeeder::class);

        $this->assertSame(
            [
                RoleCode::Admin->value,
                RoleCode::AdminApp->value,
                RoleCode::Scanner->value,
                RoleCode::User->value,
            ],
            Role::query()->orderBy('codigo')->pluck('codigo')->all()
        );
        $this->assertCount(22, Permission::query()->get());
    }

    public function test_it_assigns_the_expected_permissions_to_each_role(): void
    {
        $this->seed(AuthorizationSeeder::class);

        $admin = Role::query()->where('codigo', RoleCode::Admin->value)->firstOrFail();
        $appAdmin = Role::query()->where('codigo', RoleCode::AdminApp->value)->firstOrFail();
        $scanner = Role::query()->where('codigo', RoleCode::Scanner->value)->firstOrFail();
        $user = Role::query()->where('codigo', RoleCode::User->value)->firstOrFail();

        $this->assertCount(22, $admin->permissions);
        $this->assertCount(0, $appAdmin->permissions);
        $this->assertSame(
            [PermissionCode::ScanTickets->value],
            $scanner->permissions->pluck('codigo')->all()
        );
        $this->assertCount(0, $user->permissions);
    }

    public function test_it_can_run_more_than_once_without_creating_duplicates(): void
    {
        $this->seed(AuthorizationSeeder::class);
        $this->seed(AuthorizationSeeder::class);

        $this->assertCount(4, Role::query()->get());
        $this->assertCount(22, Permission::query()->get());
        $this->assertDatabaseCount('roles_permisos', 23);
    }
}
