<?php

namespace Database\Seeders;

use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Get roles
        $roles = Role::all()->keyBy('slug');

        if ($roles->isEmpty()) {
            $this->command->error('Roles not found. Please run RoleSeeder first.');

            return;
        }

        $users = [
            // Super Admins
            [
                'name' => 'John Smith',
                'email' => 'john.smith@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'super-admin',
            ],
            // Admins
            [
                'name' => 'Sarah Johnson',
                'email' => 'sarah.johnson@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'admin',
            ],
            [
                'name' => 'Stephanie Walker',
                'email' => 'stephanie.walker@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'admin',
            ],
            // Sales Managers
            [
                'name' => 'Michael Chen',
                'email' => 'michael.chen@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'sales-manager',
            ],
            // Sales Representatives
            [
                'name' => 'Robert Wilson',
                'email' => 'robert.wilson@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'sales-rep',
            ],
            [
                'name' => 'David Martinez',
                'email' => 'david.martinez@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'sales-rep',
            ],
            [
                'name' => 'Justin King',
                'email' => 'justin.king@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'sales-rep',
            ],
            // Accountants
            [
                'name' => 'Emily Davis',
                'email' => 'emily.davis@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'accountant',
            ],
            [
                'name' => 'Nicole Clark',
                'email' => 'nicole.clark@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'accountant',
            ],
            // Finance Manager
            [
                'name' => 'James Taylor',
                'email' => 'james.taylor@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'finance-manager',
            ],
            // Inventory Manager
            [
                'name' => 'Lisa Anderson',
                'email' => 'lisa.anderson@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'inventory-manager',
            ],
            // Warehouse Staff
            [
                'name' => 'Kevin Harris',
                'email' => 'kevin.harris@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'warehouse-staff',
            ],
            [
                'name' => 'Daniel Robinson',
                'email' => 'daniel.robinson@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'warehouse-staff',
            ],
            // HR Manager
            [
                'name' => 'Jennifer Brown',
                'email' => 'jennifer.brown@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'hr-manager',
            ],
            // Customer Support
            [
                'name' => 'Amanda White',
                'email' => 'amanda.white@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'customer-support',
            ],
            [
                'name' => 'Ashley Thompson',
                'email' => 'ashley.thompson@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'customer-support',
            ],
            // Marketing Manager
            [
                'name' => 'Michelle Garcia',
                'email' => 'michelle.garcia@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'marketing-manager',
            ],
            // IT Support
            [
                'name' => 'Brandon Lewis',
                'email' => 'brandon.lewis@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'it-support',
            ],
            [
                'name' => 'Christopher Lee',
                'email' => 'christopher.lee@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'it-support',
            ],
            // Operations Manager
            [
                'name' => 'Andrew Mitchell',
                'email' => 'andrew.mitchell@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'operations-manager',
            ],
            // Viewers
            [
                'name' => 'Rachel Scott',
                'email' => 'rachel.scott@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'viewer',
            ],
            [
                'name' => 'Tyler Adams',
                'email' => 'tyler.adams@proinvoice.com',
                'password' => 'password123',
                'role_slug' => 'viewer',
            ],
            // Customer Portal Users
            [
                'name' => 'Customer Demo',
                'email' => 'customer@demo.com',
                'password' => 'password123',
                'role_slug' => 'customer',
            ],
        ];

        foreach ($users as $userData) {
            $role = $roles->get($userData['role_slug']);

            if (! $role) {
                continue;
            }

            User::updateOrCreate(
                ['email' => $userData['email']],
                [
                    'name' => $userData['name'],
                    'email' => $userData['email'],
                    'password' => Hash::make($userData['password']),
                    'role_id' => $role->id,
                    'email_verified_at' => now(),
                ]
            );
        }

        $this->command->newLine();
        $this->command->info('👤 Demo Users Created:');
        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
        $this->command->info('Super Admin: john.smith@proinvoice.com / password123');
        $this->command->info('Admin: sarah.johnson@proinvoice.com / password123');
        $this->command->info('Customer: customer@demo.com / password123');
        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
        $this->command->info('All demo users share password: password123');
        $this->command->newLine();
    }
}
