<?php

namespace App\Exports;

use App\Models\Attendance;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class AttendanceExport implements FromCollection, WithHeadings, WithMapping
{
    public function collection()
    {
        return Attendance::with('user')->orderBy('date', 'desc')->get();
    }

    public function headings(): array
    {
        return [
            'Employee Name',
            'Date',
            'Check In',
            'Check Out',
            'Worked Minutes',
            'Break Minutes',
            'Status',
            'Type'
        ];
    }

    public function map($attendance): array
    {
        return [
            $attendance->user->name,
            $attendance->date->format('Y-m-d'),
            $attendance->check_in ? $attendance->check_in->format('H:i') : '-',
            $attendance->check_out ? $attendance->check_out->format('H:i') : '-',
            $attendance->total_work_minutes,
            $attendance->total_break_minutes,
            ucfirst($attendance->status),
            $attendance->is_manual ? 'Manual' : 'System'
        ];
    }
}
