<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Restaurant;
use App\Models\Branch;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Get the first restaurant and branch
        $restaurant = Restaurant::first();
        $branch = Branch::first();

        if (!$restaurant || !$branch) {
            $this->command->error('Please run RestaurantSeeder first to create restaurant and branch data.');
            return;
        }

        // Create default users with different roles
        $users = [
            [
                'name' => 'System Administrator',
                'email' => 'admin@xpats.com',
                'password' => Hash::make('admin123'),
                'role' => 'admin',
                'restaurant_id' => $restaurant->id,
                'branch_id' => null, // Admin can access all branches
                'phone' => '+254700000001',
                'employee_id' => 'EMP001',
                'department' => 'Administration',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('admin'),
            ],
            [
                'name' => 'Branch Manager',
                'email' => 'manager@xpats.com',
                'password' => Hash::make('manager123'),
                'role' => 'manager',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000002',
                'employee_id' => 'EMP002',
                'department' => 'Management',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('manager'),
            ],
            [
                'name' => 'Head Cashier',
                'email' => 'cashier@xpats.com',
                'password' => Hash::make('cashier123'),
                'role' => 'cashier',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000003',
                'employee_id' => 'EMP003',
                'department' => 'Front of House',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('cashier'),
            ],
            [
                'name' => 'Senior Waiter',
                'email' => 'waiter@xpats.com',
                'password' => Hash::make('waiter123'),
                'role' => 'waiter',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000004',
                'employee_id' => 'EMP004',
                'department' => 'Front of House',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('waiter'),
            ],
            [
                'name' => 'Head Chef',
                'email' => 'chef@xpats.com',
                'password' => Hash::make('chef123'),
                'role' => 'kitchen',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000005',
                'employee_id' => 'EMP005',
                'department' => 'Kitchen',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('kitchen'),
            ],
            [
                'name' => 'Inventory Manager',
                'email' => 'inventory@xpats.com',
                'password' => Hash::make('inventory123'),
                'role' => 'inventory_manager',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000006',
                'employee_id' => 'EMP006',
                'department' => 'Operations',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('inventory_manager'),
            ],
            [
                'name' => 'Accountant',
                'email' => 'accountant@xpats.com',
                'password' => Hash::make('accountant123'),
                'role' => 'accountant',
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'phone' => '+254700000007',
                'employee_id' => 'EMP007',
                'department' => 'Finance',
                'is_active' => true,
                'permissions' => $this->getDefaultPermissions('accountant'),
            ],
        ];

        foreach ($users as $userData) {
            User::updateOrCreate(
                ['email' => $userData['email']],
                $userData
            );
        }

        $this->command->info('Default users created successfully!');
        $this->command->info('Login credentials:');
        $this->command->info('Admin: admin@xpats.com / admin123');
        $this->command->info('Manager: manager@xpats.com / manager123');
        $this->command->info('Cashier: cashier@xpats.com / cashier123');
        $this->command->info('Waiter: waiter@xpats.com / waiter123');
        $this->command->info('Chef: chef@xpats.com / chef123');
        $this->command->info('Inventory: inventory@xpats.com / inventory123');
        $this->command->info('Accountant: accountant@xpats.com / accountant123');
    }

    private function getDefaultPermissions(string $role): array
    {
        return match($role) {
            'admin' => [
                'access_pos', 'process_payments', 'manage_tables', 'view_orders', 'modify_orders', 'cancel_orders',
                'access_kitchen', 'update_order_status', 'view_kitchen_tickets',
                'view_inventory', 'manage_inventory', 'receive_stock', 'adjust_stock',
                'view_menu', 'manage_menu', 'update_prices',
                'view_reports', 'view_financial_reports', 'export_reports',
                'manage_users', 'view_user_activity',
                'manage_settings', 'manage_branches', 'manage_customers',
            ],
            'manager' => [
                'access_pos', 'process_payments', 'manage_tables', 'view_orders', 'modify_orders', 'cancel_orders',
                'access_kitchen', 'update_order_status', 'view_kitchen_tickets',
                'view_inventory', 'manage_inventory', 'receive_stock', 'adjust_stock',
                'view_menu', 'manage_menu', 'update_prices',
                'view_reports', 'view_financial_reports', 'export_reports',
                'manage_users', 'view_user_activity',
                'manage_customers',
            ],
            'cashier' => [
                'access_pos', 'process_payments', 'manage_tables', 'view_orders', 'modify_orders',
                'view_inventory', 'manage_customers',
            ],
            'waiter' => [
                'access_pos', 'process_payments', 'manage_tables', 'view_orders', 'modify_orders',
                'manage_customers', 'view_own_sales', 'view_waiter_reports',
            ],
            'kitchen' => [
                'access_kitchen', 'update_order_status', 'view_kitchen_tickets',
                'view_menu', 'view_inventory',
            ],
            'inventory_manager' => [
                'view_inventory', 'manage_inventory', 'receive_stock', 'adjust_stock',
                'view_reports', 'export_reports',
            ],
            'accountant' => [
                'view_orders', 'view_reports', 'view_financial_reports', 'export_reports',
                'view_inventory',
            ],
            default => [],
        };
    }
}