<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;

class AddManagerRoleSeeder extends Seeder
{
    public function run(): void
    {
        // Ensure the Manager role exists
        $manager = Role::firstOrCreate(
            ['slug' => 'manager'],
            [
                'name' => 'Manager',
                'description' => 'Team manager with elevated permissions',
            ]
        );

        // Give Manager all permissions except sensitive settings management (idempotent)
        // If permissions are not seeded yet, this will simply sync an empty set.
        $permissionIds = Permission::query()
            ->where('slug', '!=', 'manage-settings')
            ->pluck('id');

        $manager->permissions()->sync($permissionIds);
    }
}
