<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Attendance;
use App\Models\AttendanceBreak;
use App\Models\WorkReport;
use App\Models\Task;
use Carbon\Carbon;

class AttendanceSeeder extends Seeder
{
    public function run(): void
    {
        $users = User::all();
        $today = Carbon::today();

        // Truncate to avoid duplicates
        \Illuminate\Support\Facades\Schema::disableForeignKeyConstraints();
        Attendance::truncate();
        AttendanceBreak::truncate();
        WorkReport::truncate();
        Task::truncate();
        \Illuminate\Support\Facades\Schema::enableForeignKeyConstraints();

        foreach ($users as $user) {
            // Generate attendance for the last 30 days (excluding weekends)
            for ($i = 1; $i <= 30; $i++) {
                $date = Carbon::today()->subDays($i);
                
                if ($date->isWeekend()) continue;

                // 90% chance of being present
                if (rand(1, 100) > 90) {
                    Attendance::create([
                        'user_id' => $user->id,
                        'date' => $date->format('Y-m-d'),
                        'status' => 'absent',
                    ]);
                    continue;
                }

                $checkIn  = (clone $date)->setHour(rand(8, 10))->setMinute(rand(0, 59));
                $checkOut = (clone $date)->setHour(rand(17, 19))->setMinute(rand(0, 59));

                $attendance = Attendance::create([
                    'user_id' => $user->id,
                    'date' => $date->format('Y-m-d'),
                    'check_in' => $checkIn,
                    'check_out' => $checkOut,
                    'total_work_minutes' => $checkOut->diffInMinutes($checkIn) - 60, // 1 hour break
                    'total_break_minutes' => 60,
                    'status' => 'present',
                ]);

                // Record break
                AttendanceBreak::create([
                    'attendance_id' => $attendance->id,
                    'break_start' => (clone $date)->setHour(13)->setMinute(0),
                    'break_end' => (clone $date)->setHour(14)->setMinute(0),
                    'duration_minutes' => 60,
                ]);

                // Work report
                $report = WorkReport::create([
                    'attendance_id' => $attendance->id,
                    'user_id' => $user->id,
                    'raw_summary' => "Completed various daily tasks on " . $date->format('Y-m-d'),
                    'total_tasks' => 3,
                    'is_submitted' => true,
                    'submitted_at' => $checkOut,
                    'approval_status' => 'approved',
                    'approved_by' => 1, // Super Admin
                    'approved_at' => (clone $checkOut)->addMinutes(30),
                ]);

                // Tasks for the report
                for ($j = 1; $j <= 3; $j++) {
                    Task::create([
                        'work_report_id' => $report->id,
                        'task_description' => "Sample task $j completed on " . $date->format('Y-m-d'),
                        'order' => $j,
                    ]);
                }
            }
        }
    }
}
