<?php

// Simple script to update waiter permissions
// Run this from the backend directory: php update-waiter-permissions.php

require_once 'vendor/autoload.php';

// Load Laravel app
$app = require_once 'bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

use App\Models\User;

try {
    // Update all waiter users to remove view_menu permission
    $waiters = User::where('role', 'waiter')->get();
    
    foreach ($waiters as $waiter) {
        $permissions = $waiter->permissions ?? [];
        
        // Remove view_menu permission if it exists
        $permissions = array_filter($permissions, function($permission) {
            return $permission !== 'view_menu';
        });
        
        // Ensure they have the correct waiter permissions
        $waiterPermissions = [
            'access_pos', 'process_payments', 'manage_tables', 'view_orders', 'modify_orders',
            'manage_customers', 'view_own_sales', 'view_waiter_reports',
        ];
        
        $waiter->update(['permissions' => $waiterPermissions]);
        
        echo "Updated permissions for waiter: {$waiter->name} ({$waiter->email})\n";
    }
    
    echo "✅ All waiter permissions updated successfully!\n";
    echo "Waiters can no longer access the Menu Management page.\n";
    
} catch (Exception $e) {
    echo "❌ Error updating permissions: " . $e->getMessage() . "\n";
}