<?php

namespace App\Filament\Exports;

use App\Models\Attendance;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class AttendanceExporter extends Exporter
{
    protected static ?string $model = Attendance::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('id')
                ->label('ID'),
            ExportColumn::make('date')
                ->label('Tanggal'),
            ExportColumn::make('user.name')
                ->label('Nama Karyawan'),
            ExportColumn::make('user.employee_id')
                ->label('NIP'),
            ExportColumn::make('company.name')
                ->label('Perusahaan'),
            ExportColumn::make('office.name')
                ->label('Kantor'),
            ExportColumn::make('type')
                ->label('Tipe')
                ->formatStateUsing(fn (string $state): string => match ($state) {
                    'office' => 'Kantor',
                    'remote' => 'Remote',
                    'business_trip' => 'Dinas',
                    default => $state,
                }),
            ExportColumn::make('check_in_time')
                ->label('Jam Masuk'),
            ExportColumn::make('check_in_latitude')
                ->label('Lat Masuk'),
            ExportColumn::make('check_in_longitude')
                ->label('Long Masuk'),
            ExportColumn::make('check_in_address')
                ->label('Alamat Masuk'),
            ExportColumn::make('check_out_time')
                ->label('Jam Pulang'),
            ExportColumn::make('check_out_latitude')
                ->label('Lat Pulang'),
            ExportColumn::make('check_out_longitude')
                ->label('Long Pulang'),
            ExportColumn::make('check_out_address')
                ->label('Alamat Pulang'),
            ExportColumn::make('work_duration')
                ->label('Durasi Kerja (Menit)'),
            ExportColumn::make('overtime_duration')
                ->label('Lembur (Menit)'),
            ExportColumn::make('status')
                ->label('Status')
                ->formatStateUsing(fn (?string $state): string => match ($state) {
                    'present' => 'Hadir',
                    'late' => 'Terlambat',
                    'early_leave' => 'Pulang Awal',
                    'absent' => 'Tidak Hadir',
                    'sick' => 'Sakit',
                    'leave' => 'Cuti',
                    'permit' => 'Izin',
                    default => $state ?? '-',
                }),
            ExportColumn::make('check_in_notes')
                ->label('Catatan Masuk'),
            ExportColumn::make('check_out_notes')
                ->label('Catatan Pulang'),
            ExportColumn::make('created_at')
                ->label('Dibuat'),
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Export presensi selesai. ' . number_format($export->successful_rows) . ' baris berhasil diexport.';

        if ($failedRowsCount = $export->getFailedRowsCount()) {
            $body .= ' ' . number_format($failedRowsCount) . ' baris gagal.';
        }

        return $body;
    }
}
