<?php

namespace Database\Seeders;

use App\Models\Department;
use App\Models\Employee;
use App\Models\EmployeeSchedule;
use App\Models\Position;
use App\Models\Shift;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class UserAndEmployeeSeeder extends Seeder
{
    public function run(): void
    {
        $defaultPassword = Hash::make('password');

        // 1. Super Admin
        $superAdminUser = User::firstOrCreate(
            ['email' => 'superadmin@absensi.test'],
            [
                'name' => 'Ismet Super Admin',
                'password' => $defaultPassword,
                'role' => 'super_admin',
                'status' => 'active',
                'avatar' => 'mazer/assets/images/faces/1.jpg',
            ]
        );

        // Also alias admin@example.com to super_admin for quick testing
        User::firstOrCreate(
            ['email' => 'admin@example.com'],
            [
                'name' => 'Administrator Utama',
                'password' => $defaultPassword,
                'role' => 'super_admin',
                'status' => 'active',
                'avatar' => 'mazer/assets/images/faces/1.jpg',
            ]
        );

        // 2. HR Admin
        $hrAdminUser = User::firstOrCreate(
            ['email' => 'admin@absensi.test'],
            [
                'name' => 'Sarah HR Admin',
                'password' => $defaultPassword,
                'role' => 'admin',
                'status' => 'active',
                'avatar' => 'mazer/assets/images/faces/2.jpg',
            ]
        );

        // 3. Departments, Positions, Shifts
        $itDept = Department::where('code', 'IT')->first() ?? Department::first();
        $hrDept = Department::where('code', 'HR')->first() ?? Department::first();
        $finDept = Department::where('code', 'FIN')->first() ?? Department::first();
        $mktDept = Department::where('code', 'MKT')->first() ?? Department::first();
        $opsDept = Department::where('code', 'OPS')->first() ?? Department::first();

        $sePos = Position::where('code', 'SE')->first() ?? Position::first();
        $hrPos = Position::where('code', 'HRS')->first() ?? Position::first();
        $finPos = Position::where('code', 'FINS')->first() ?? Position::first();
        $mktPos = Position::where('code', 'MKTS')->first() ?? Position::first();
        $opsPos = Position::where('code', 'OPSV')->first() ?? Position::first();

        $pagiShift = Shift::where('name', 'like', '%Pagi%')->first() ?? Shift::first();
        $siangShift = Shift::where('name', 'like', '%Siang%')->first() ?? Shift::first();

        // 4. Create Karyawan Demo Users & Employees
        $employeesData = [
            [
                'name' => 'Ismet Fauzi',
                'email' => 'employee@absensi.test',
                'nik' => 'EMP-2026-001',
                'phone' => '085196026704',
                'gender' => 'male',
                'birth_place' => 'Jakarta',
                'birth_date' => '1995-04-12',
                'address' => 'Jl. Tebet Barat No. 15, Jakarta Selatan',
                'dept_id' => $itDept?->id,
                'pos_id' => $sePos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/3.jpg',
            ],
            [
                'name' => 'Siti Rahmawati',
                'email' => 'employee@example.com',
                'nik' => 'EMP-2026-002',
                'phone' => '081234567891',
                'gender' => 'female',
                'birth_place' => 'Bandung',
                'birth_date' => '1997-08-20',
                'address' => 'Jl. Dago Asri No. 44, Bandung',
                'dept_id' => $hrDept?->id,
                'pos_id' => $hrPos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/5.jpg',
            ],
            [
                'name' => 'Budi Santoso',
                'email' => 'budi.santoso@absensi.test',
                'nik' => 'EMP-2026-003',
                'phone' => '081234567892',
                'gender' => 'male',
                'birth_place' => 'Surabaya',
                'birth_date' => '1993-11-05',
                'address' => 'Jl. Rungkut Madya No. 8, Surabaya',
                'dept_id' => $itDept?->id,
                'pos_id' => $sePos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/4.jpg',
            ],
            [
                'name' => 'Dewi Lestari',
                'email' => 'dewi.lestari@absensi.test',
                'nik' => 'EMP-2026-004',
                'phone' => '081234567893',
                'gender' => 'female',
                'birth_place' => 'Semarang',
                'birth_date' => '1996-02-14',
                'address' => 'Jl. Pandanaran No. 102, Semarang',
                'dept_id' => $finDept?->id,
                'pos_id' => $finPos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/8.jpg',
            ],
            [
                'name' => 'Eko Prasetyo',
                'email' => 'eko.prasetyo@absensi.test',
                'nik' => 'EMP-2026-005',
                'phone' => '081234567894',
                'gender' => 'male',
                'birth_place' => 'Yogyakarta',
                'birth_date' => '1994-09-30',
                'address' => 'Jl. Malioboro No. 50, Yogyakarta',
                'dept_id' => $opsDept?->id,
                'pos_id' => $opsPos?->id,
                'shift_id' => $siangShift?->id,
                'avatar' => 'mazer/assets/images/faces/7.jpg',
            ],
            [
                'name' => 'Fitri Handayani',
                'email' => 'fitri.handayani@absensi.test',
                'nik' => 'EMP-2026-006',
                'phone' => '081234567895',
                'gender' => 'female',
                'birth_place' => 'Malang',
                'birth_date' => '1998-05-18',
                'address' => 'Jl. Ijen No. 25, Malang',
                'dept_id' => $mktDept?->id,
                'pos_id' => $mktPos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/6.jpg',
            ],
            [
                'name' => 'Gilang Ramadhan',
                'email' => 'gilang.ramadhan@absensi.test',
                'nik' => 'EMP-2026-007',
                'phone' => '081234567896',
                'gender' => 'male',
                'birth_place' => 'Medan',
                'birth_date' => '1992-07-22',
                'address' => 'Jl. Gatot Subroto No. 88, Medan',
                'dept_id' => $itDept?->id,
                'pos_id' => $sePos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/1.jpg',
            ],
            [
                'name' => 'Hani Pratiwi',
                'email' => 'hani.pratiwi@absensi.test',
                'nik' => 'EMP-2026-008',
                'phone' => '081234567897',
                'gender' => 'female',
                'birth_place' => 'Solo',
                'birth_date' => '1999-01-10',
                'address' => 'Jl. Slamet Riyadi No. 12, Surakarta',
                'dept_id' => $finDept?->id,
                'pos_id' => $finPos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/5.jpg',
            ],
            [
                'name' => 'Indra Gunawan',
                'email' => 'indra.gunawan@absensi.test',
                'nik' => 'EMP-2026-009',
                'phone' => '081234567898',
                'gender' => 'male',
                'birth_place' => 'Palembang',
                'birth_date' => '1995-12-03',
                'address' => 'Jl. Jend. Sudirman No. 45, Palembang',
                'dept_id' => $opsDept?->id,
                'pos_id' => $opsPos?->id,
                'shift_id' => $siangShift?->id,
                'avatar' => 'mazer/assets/images/faces/2.jpg',
            ],
            [
                'name' => 'Joko Widodo',
                'email' => 'joko.widodo@absensi.test',
                'nik' => 'EMP-2026-010',
                'phone' => '081234567899',
                'gender' => 'male',
                'birth_place' => 'Surakarta',
                'birth_date' => '1991-06-21',
                'address' => 'Jl. Letjen Suprapto No. 1, Surakarta',
                'dept_id' => $mktDept?->id,
                'pos_id' => $mktPos?->id,
                'shift_id' => $pagiShift?->id,
                'avatar' => 'mazer/assets/images/faces/3.jpg',
            ],
        ];

        foreach ($employeesData as $empData) {
            $user = User::firstOrCreate(
                ['email' => $empData['email']],
                [
                    'name' => $empData['name'],
                    'password' => $defaultPassword,
                    'role' => 'employee',
                    'status' => 'active',
                    'avatar' => $empData['avatar'],
                ]
            );

            $employee = Employee::firstOrCreate(
                ['user_id' => $user->id],
                [
                    'nik' => $empData['nik'],
                    'name' => $empData['name'],
                    'email' => $empData['email'],
                    'phone' => $empData['phone'],
                    'gender' => $empData['gender'],
                    'birth_place' => $empData['birth_place'],
                    'birth_date' => $empData['birth_date'],
                    'address' => $empData['address'],
                    'department_id' => $empData['dept_id'],
                    'position_id' => $empData['pos_id'],
                    'join_date' => '2024-01-01',
                    'employment_status' => 'permanent',
                    'photo' => $empData['avatar'],
                    'status' => 'active',
                    'leave_balance' => 12,
                ]
            );

            // Assign active schedule
            EmployeeSchedule::firstOrCreate(
                [
                    'employee_id' => $employee->id,
                    'shift_id' => $empData['shift_id'],
                ],
                [
                    'effective_from' => '2024-01-01',
                    'is_active' => true,
                ]
            );
        }
    }
}
