<?php

use App\Enums\PermissionSlug;
use App\Enums\RoleSlug;
use App\Models\Permission;
use App\Models\Role;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;

uses(LazilyRefreshDatabase::class);

it('seeds roles and permissions without duplicates', function () {
    $this->seed([
        PermissionSeeder::class,
        RoleSeeder::class,
    ]);

    $this->seed([
        PermissionSeeder::class,
        RoleSeeder::class,
    ]);

    $admin = Role::query()->where('slug', RoleSlug::Admin)->first();
    $manager = Role::query()->where('slug', RoleSlug::Manager)->first();

    expect(Role::query()->count())->toBe(count(RoleSlug::cases()))
        ->and(Permission::query()->count())->toBe(count(PermissionSlug::cases()))
        ->and($admin)->not->toBeNull()
        ->and($manager)->not->toBeNull()
        ->and($admin->permissions()->count())->toBe(count(PermissionSlug::cases()))
        ->and($manager->permissions()->pluck('slug')->all())->toEqualCanonicalizing(
            RoleSlug::Manager->permissions(),
        );
});
