<?php

namespace Database\Seeders;

use App\Models\DressCode;
use App\Models\MaritalStatus;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\UserDetail;
use App\Models\UserPreference;
use Illuminate\Support\Facades\Hash;
use Faker\Factory as Faker;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $faker = Faker::create();

        for ($i = 0; $i < 100; $i++) {
            $this->createUser($faker);
        }

        
    }

    private function createUser($faker)
    {
        
        // Create a user
        $user = User::create([
            'name' => $faker->name,
            'nick_name' => $faker->name,
            'email' => $faker->unique()->safeEmail,
            'password' => Hash::make('password'), // Default password
            'dial_code' => $faker->randomElement(['+1', '+44', '+91']),
            'phone' => $faker->unique()->randomNumber(9),
            'country_id' => $faker->numberBetween(1, 200),
            'status' => 1,
            'role_id' => $faker->numberBetween(1, 5),
            'gender' => $faker->randomElement(['Male', 'Female']),
            'dob' => $faker->date('Y-m-d', '2000-01-01'),
            'profile_image' => $faker->imageUrl(),
            'is_verified' => $faker->boolean,
            'user_number' => 'LQ'.$faker->unique()->randomNumber(6),
        ]);

        // Create user details
        UserDetail::create([
            'user_id' => $user->id,
            'sect_id' => $faker->numberBetween(1, 10),
            'education_id' => $faker->numberBetween(1, 7),
            'profession_id' => $faker->numberBetween(1, 5),
            'height_cm' => $faker->numberBetween(150, 200),
            'height_ft' => $faker->randomFloat(2, 4.9, 6.5),
            'weight_kg' => $faker->numberBetween(50, 100),
            'weight_lbs' => $faker->numberBetween(110, 220),
            'marital_status_id' => $faker->numberBetween(1, 7),
            'wives' => $faker->numberBetween(0, 3),
            'children' => $faker->numberBetween(0, 10),
            'religious_practice' => $faker->numberBetween(1, 100),
            'dress_code_id' => $faker->numberBetween(1, 5),
            'smoking' => $faker->boolean,
            'genetic_disease' => $faker->boolean,
            'genetic_disease_id' => $faker->numberBetween(1, 10),
            'about' => $faker->paragraph,
        ]);

        // Create user preferences
        UserPreference::create([
            'user_id' => $user->id,
            'sect_id' => $faker->numberBetween(1, 10),
            'education_id' => $faker->numberBetween(1, 10),
            'profession_id' => $faker->numberBetween(1, 10),
            'weight_all' => $faker->numberBetween(0, 1),
            'height_all' => $faker->numberBetween(0, 1),
            'from_height_cm' => $faker->numberBetween(150, 160),
            'to_height_cm' => $faker->numberBetween(170, 200),
            'from_height_ft' => $faker->randomFloat(2, 4.9, 5.2),
            'to_height_ft' => $faker->randomFloat(2, 5.5, 6.5),
            'from_weight_kg' => $faker->numberBetween(50, 60),
            'to_weight_kg' => $faker->numberBetween(70, 100),
            'from_weight_lbs' => $faker->numberBetween(110, 130),
            'to_weight_lbs' => $faker->numberBetween(150, 220),
            'religion_practice' => $faker->numberBetween(1, 100),
            'weight_all' => $faker->boolean,
            'height_all' => $faker->boolean,
            'smoking' => $faker->boolean,
        ]);

        $maritalStatus = MaritalStatus::all();
        $dressCode = DressCode::all();

        // Create records for UserMaritalStatus
        foreach ($maritalStatus->random(rand(1, $maritalStatus->count())) as $status) {
            $user->maritalStatuses()->create([
            'marital_status_id' => $status->id,
            ]);
        }

        // Create records for UserDressCode
        foreach ($dressCode->random(rand(1, $dressCode->count())) as $code) {
            $user->dressCodes()->create([
            'dress_code_id' => $code->id,
            ]);
        }
    }
}
