<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

class SetupWaiterPins extends Command
{
    protected $signature = 'waiters:setup-pins';
    protected $description = 'Setup PINs for all active waiters';

    public function handle()
    {
        $this->info('Setting up waiter PINs...');
        
        $waiters = User::where('role', 'waiter')
            ->where('is_active', true)
            ->get();
        
        if ($waiters->isEmpty()) {
            $this->warn('No active waiters found.');
            return;
        }
        
        $this->info("Found {$waiters->count()} active waiters:");
        
        foreach ($waiters as $waiter) {
            // Generate a 4-digit PIN based on user ID
            $pin = str_pad(($waiter->id * 1111) % 10000, 4, '0', STR_PAD_LEFT);
            
            // Update the waiter with the PIN
            $waiter->update(['pin' => $pin]);
            
            $this->line("- {$waiter->name} (ID: {$waiter->id}) - PIN: {$pin}");
        }
        
        $this->info('Waiter PINs have been set up successfully!');
        $this->info('Waiters can now login using their PIN on the waiter login screen.');
    }
}