<?php

namespace Database\Seeders;

use App\Models\Attendance;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class AttendanceSeeder extends Seeder
{
    public function run(): void
    {
        $employees = User::where('type', 'employee')->get();
        $admin = User::where('type', 'super-admin')->first();

        foreach ($employees as $employee) {
            // Seed last 30 days
            for ($i = 0; $i < 30; $i++) {
                $date = Carbon::today()->subDays($i);

                // Skip weekends for demo realism (mostly)
                if ($date->isWeekend() && rand(0, 10) > 2) {
                    continue;
                }

                $clockIn = (clone $date)->setTime(rand(8, 10), rand(0, 59));
                $clockOut = (clone $clockIn)->addHours(rand(8, 10));

                Attendance::create([
                    'user_id' => $employee->id,
                    'date' => $date->toDateString(),
                    'clock_in' => $clockIn,
                    'clock_out' => $clockOut,
                    'total_hours' => $clockIn->diffInMinutes($clockOut) / 60,
                    'status' => 'approved',
                    'work_mode' => rand(0, 10) > 2 ? 'office' : 'remote',
                    'admin_approved_by' => $admin?->id,
                    'admin_approved_at' => now(),
                ]);
            }
        }
    }
}
