<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class EmployeeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $engineering = \App\Models\Department::where('name', 'Engineering')->first();
        $marketing = \App\Models\Department::where('name', 'Marketing')->first();
        $product = \App\Models\Department::where('name', 'Product')->first();
        $business = \App\Models\Department::where('name', 'Business')->first();

        $depts = [$engineering, $marketing, $product, $business];

        $employees = [
            ['name' => 'Alex Rivera', 'email' => 'employee1@gmail.com', 'title' => 'Technical Lead', 'dept' => $engineering],
            ['name' => 'Sarah Chen', 'email' => 'employee2@gmail.com', 'title' => 'Senior Developer', 'dept' => $engineering],
            ['name' => 'Michael Scott', 'email' => 'employee3@gmail.com', 'title' => 'Product Manager', 'dept' => $product],
            ['name' => 'Jessica Alba', 'email' => 'employee4@gmail.com', 'title' => 'Marketing Specialist', 'dept' => $marketing],
            ['name' => 'David Miller', 'email' => 'employee5@gmail.com', 'title' => 'Backend Developer', 'dept' => $engineering],
            ['name' => 'Emily Blunt', 'email' => 'employee6@gmail.com', 'title' => 'UI/UX Designer', 'dept' => $product],
            ['name' => 'Zuck Maverick', 'email' => 'employee7@gmail.com', 'title' => 'HR Operations', 'dept' => $business],
            ['name' => 'John Wick', 'email' => 'employee8@gmail.com', 'title' => 'Content Strategist', 'dept' => $marketing],
            ['name' => 'Robert Deniro', 'email' => 'employee9@gmail.com', 'title' => 'QA Engineer', 'dept' => $engineering],
            ['name' => 'Test Employee', 'email' => 'employee10@gmail.com', 'title' => 'Junior Developer', 'dept' => $engineering],
        ];

        $managerId = null;

        foreach ($employees as $index => $emp) {
            $user = \App\Models\User::updateOrCreate(
                ['email' => $emp['email']],
                [
                    'name' => $emp['name'],
                    'password' => \Illuminate\Support\Facades\Hash::make('12345678'),
                    'type' => \App\Enums\UserType::EMPLOYEE,
                    'email_verified_at' => now(),
                    'status' => 'active',
                ]
            );

            if ($index === 0) {
                $managerId = $user->id;
            }

            $user->employeeProfile()->updateOrCreate(
                ['user_id' => $user->id],
                [
                    'job_title' => $emp['title'],
                    'department_id' => $emp['dept']?->id,
                    'manager_id' => ($index > 0 && $index < 5) ? $managerId : null,
                    'joining_date' => now()->subMonths(rand(1, 24)),
                    'phone' => '01' . rand(100000000, 999999999),
                    'address' => 'Dhaka, Bangladesh',
                    'city' => 'Dhaka',
                ]
            );
        }
    }
}
