<?php

namespace Database\Seeders;

use App\Models\LeaveRequest;
use App\Models\LeaveType;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class LeaveRequestSeeder extends Seeder
{
    public function run(): void
    {
        $employees = User::where('type', 'employee')->get();
        $admin = User::where('type', 'super-admin')->first();
        $sickLeave = LeaveType::where('name', 'Sick Leave')->first();
        $casualLeave = LeaveType::where('name', 'Casual Leave')->first();

        foreach ($employees as $index => $employee) {
            // Past approved leave
            LeaveRequest::create([
                'user_id' => $employee->id,
                'leave_type_id' => $sickLeave->id,
                'start_date' => Carbon::today()->subMonths(1),
                'end_date' => Carbon::today()->subMonths(1)->addDays(2),
                'total_days' => 3,
                'reason' => 'Recovery from seasonal flu',
                'status' => 'approved',
                'admin_approved_by' => $admin->id,
                'admin_approved_at' => now()->subMonths(1),
                'admin_comment' => 'Approved. Get well soon.',
            ]);

            // Upcoming pending leave for some
            if ($index % 3 === 0) {
                LeaveRequest::create([
                    'user_id' => $employee->id,
                    'leave_type_id' => $casualLeave->id,
                    'start_date' => Carbon::today()->addDays(7),
                    'end_date' => Carbon::today()->addDays(10),
                    'total_days' => 4,
                    'reason' => 'Family event and rest',
                    'status' => 'pending_manager',
                    'manager_id' => $employee->employeeProfile?->manager_id,
                ]);
            }
        }
    }
}
