<?php

namespace Database\Seeders;

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

class RoleSeeder extends Seeder
{
    public function run(): void
    {
        // Super Admin - All permissions
        $superAdmin = Role::firstOrCreate([
            'name' => 'super-admin',
            'guard_name' => 'sanctum',
        ]);
        $superAdmin->syncPermissions(Permission::all());

        // Branch Admin - All permissions except branch management and bowl settings
        $branchAdmin = Role::firstOrCreate([
            'name' => 'branch-admin',
            'guard_name' => 'sanctum',
        ]);
        $branchAdminPermissions = Permission::whereNotIn('name', [
            'branches.create', 'branches.delete',
            'roles.create', 'roles.update', 'roles.delete',
            'batch-size-configs.create', 'batch-size-configs.update', 'batch-size-configs.delete',
        ])->pluck('name')->toArray();
        $branchAdmin->syncPermissions($branchAdminPermissions);

        // Manager - Most permissions (no bowl settings management)
        $manager = Role::firstOrCreate([
            'name' => 'manager',
            'guard_name' => 'sanctum',
        ]);
        $managerPermissions = Permission::whereNotIn('name', [
            'branches.create', 'branches.update', 'branches.delete',
            'roles.create', 'roles.update', 'roles.delete', 'roles.assign',
            'permissions.assign',
            'users.delete',
            'batch-size-configs.create', 'batch-size-configs.update', 'batch-size-configs.delete',
        ])->pluck('name')->toArray();
        $manager->syncPermissions($managerPermissions);

        // POS Operator - Limited to POS and order operations
        $posOperator = Role::firstOrCreate([
            'name' => 'pos-operator',
            'guard_name' => 'sanctum',
        ]);
        $posOperator->syncPermissions([
            'pos.access', 'pos.create-order',
            'orders.view', 'orders.create', 'orders.update',
            'customers.view', 'customers.create', 'customers.update',
            'products.view',
            'categories.view',
            'option-sets.view',
            'inner-option-sets.view',
            'recipes.view',
            'ingredients.view',
            'stock.view',
        ]);

        // Kitchen Staff - View orders, update status
        $kitchenStaff = Role::firstOrCreate([
            'name' => 'kitchen-staff',
            'guard_name' => 'sanctum',
        ]);
        $kitchenStaff->syncPermissions([
            'orders.view', 'orders.update',
            'products.view',
            'recipes.view',
            'ingredients.view',
            'stock.view',
        ]);
    }
}
