<?php

namespace Database\Seeders;

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

class NovaRolesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Ensure permissions cache is cleared
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define role names (machine-friendly)
        $roles = [
            'owner',                 // Administrator/Owner – full access
            'accountant',            // finance, payments, invoices
            'sales_officer',         // creates sales orders, manages customers
            'purchaser',             // creates PRs, generates POs, follows up
            'inventory_manager',     // stock, warehouses, IGP, scrap
            'production_officer',    // manufacturing orders, BOM
            'qc_officer',            // QC approvals
            'it_admin',              // IT/ERP Administrator (no operational)
            'project_manager',       // supervises across modules
            'shop_manager',          // retail POS
        ];

        foreach ($roles as $roleName) {
            Role::firstOrCreate([
                'name' => $roleName,
                'guard_name' => 'web',
            ]);
        }

        // Optional: Give all permissions to owner if permissions already exist
        $owner = Role::where('name', 'owner')->first();
        if ($owner && Permission::count() > 0) {
            $owner->syncPermissions(Permission::all());
        }
    }
}


