<?php

use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

return new class extends Migration {
    public function up(): void
    {
        app(PermissionRegistrar::class)->forgetCachedPermissions();
        foreach (['view', 'create', 'update', 'delete'] as $action) {
            Permission::firstOrCreate(['name' => "{$action}_notes", 'guard_name' => 'web']);
        }
        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }

    public function down(): void
    {
        Permission::whereIn('name', ['view_notes', 'create_notes', 'update_notes', 'delete_notes'])->delete();
        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
};
