<?php

namespace Database\Seeders;

use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        DB::disableQueryLog();
        Model::unsetEventDispatcher();

        $chunkSize = 100;
        $tableName = 'users';

        $hashedPassword = Hash::make('password');
        $now = CarbonImmutable::now();
        $startDate = $now->subMonths(6);

        $total = 500;
        $rows = [];

        $firstNames = ['Ahmed', 'Mohammed', 'Ali', 'Omar', 'Khaled', 'Sara', 'Fatima', 'Nora', 'Layla', 'Hana', 'Youssef', 'Hassan', 'Mona', 'Reem', 'Salma'];
        $lastNames = ['Ali', 'Mohammed', 'Hassan', 'Ahmad', 'Salem', 'Abdullah', 'Ibrahim', 'Khalid', 'Nasser', 'Saeed'];

        for ($i = 0; $i < $total; $i++) {
            $firstName = $firstNames[array_rand($firstNames)];
            $lastName = $lastNames[array_rand($lastNames)];

            $createdAt = CarbonImmutable::createFromTimestamp(
                rand($startDate->timestamp, $now->timestamp)
            );

            $rows[] = [
                'name'              => $firstName . ' ' . $lastName,
                'email'             => strtolower($firstName) . $i . '@example.com',
                'phone'             => '05' . str_pad(rand(0, 99999999), 8, '0', STR_PAD_LEFT),
                'password'          => $hashedPassword,
                'email_verified_at' => rand(0, 1) ? $createdAt : null,
                'created_at'        => $createdAt,
                'updated_at'        => $createdAt,
            ];

            if (count($rows) >= $chunkSize) {
                DB::table($tableName)->insert($rows);
                $rows = [];
            }
        }

        if (!empty($rows)) {
            DB::table($tableName)->insert($rows);
        }

        $this->command->info("Users seeded: {$total} records");
    }
}
