<?php

namespace Database\Seeders;

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

class RolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        $permissions = [
            'view_suppliers',
            'create_suppliers',
            'edit_suppliers',
            'delete_suppliers',

            'view_products',
            'create_products',
            'edit_products',
            'delete_products',

            'view_orders',
            'create_orders',
            'edit_orders',
            'delete_orders',

            'view_purchase_orders',
            'create_purchase_orders',
            'edit_purchase_orders',
            'delete_purchase_orders',

            'manage_point_settings',
            'view_financial_reports',
            'manage_users',
        ];

        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission]);
        }

        $admin = Role::firstOrCreate(['name' => 'admin']);
        $admin->syncPermissions($permissions);

        $storeManager = Role::firstOrCreate(['name' => 'store_manager']);
        $storeManager->syncPermissions([
            'view_suppliers',
            'view_products',
            'create_products',
            'edit_products',
            'view_purchase_orders',
            'create_purchase_orders',
            'edit_purchase_orders',
        ]);

        $salesAgent = Role::firstOrCreate(['name' => 'sales_agent']);
        $salesAgent->syncPermissions([
            'view_orders',
            'edit_orders',
        ]);
    }
}
