<?php
  
namespace Database\Seeders;
  
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
  
class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $permissions = [
            'song-list',
            'song-create',
            'song-edit',
            'song-delete',
            'room-list',
            'room-create',
            'room-edit',
            'room-delete',
            'user-list',
            'user-create',
            'user-edit',
            'user-delete',
            'role-list',
            'role-create',
            'role-edit',
            'role-delete'
        ];
        
        foreach ($permissions as $permission) {
             Permission::create(['name' => $permission]);
        }
        // 建立 Admin 角色，給所有權限
        $adminRole = Role::firstOrCreate(['name' => 'Admin']);
        $adminRole->syncPermissions(Permission::all());

        $machineRole = Role::firstOrCreate(['name' => 'Machine']);
        $machineRole->syncPermissions([
            'room-list',
            'room-create',
            'room-edit',
        ]);
        // 建立 User 角色，不給任何權限
        Role::firstOrCreate(['name' => 'User']);
    }
}
