<?php

namespace Database\Seeders;

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

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Define permissions your app actually uses
        $permissions = [
            'view dashboard',
            'manage courses',
            'manage users',
        ];

        foreach ($permissions as $perm) {
            Permission::firstOrCreate(['name' => $perm, 'guard_name' => 'web']);
        }

        // Assign to roles
        $admin   = Role::where('name', 'admin')->first();
        $teacher = Role::where('name', 'teacher')->first();
        $student = Role::where('name', 'student')->first();

        if ($admin) {
            $admin->givePermissionTo($permissions); // admin gets all
        }
        if ($teacher) {
            $teacher->givePermissionTo(['view dashboard', 'manage courses']);
        }
        if ($student) {
            $student->givePermissionTo(['view dashboard']);
        }
    }
}