<?php

declare(strict_types=1);

namespace WorkforceOS\Services;

final class AttendanceService
{
    public function today(array $tenant): array
    {
        return [
            'tenant_id' => $tenant['id'],
            'date' => date('Y-m-d'),
            'records' => [
                [
                    'employee_code' => 'EMP-1001',
                    'check_in' => '08:56:22',
                    'lunch_out' => '13:00:01',
                    'lunch_in' => '13:41:18',
                    'check_out' => null,
                    'late_minutes' => 0,
                    'early_out_minutes' => 0,
                    'working_minutes' => 244,
                    'overtime_minutes' => 0,
                    'status' => 'present',
                ],
                [
                    'employee_code' => 'EMP-1007',
                    'check_in' => '09:31:10',
                    'lunch_out' => null,
                    'lunch_in' => null,
                    'check_out' => null,
                    'late_minutes' => 31,
                    'early_out_minutes' => 0,
                    'working_minutes' => 210,
                    'overtime_minutes' => 0,
                    'status' => 'late',
                ],
            ],
        ];
    }

    public function ingestLog(array $tenant, array $payload): array
    {
        $required = ['employee_code', 'device_serial', 'punch_time', 'punch_type'];
        foreach ($required as $field) {
            if (empty($payload[$field])) {
                return ['accepted' => false, 'error' => "Missing field: {$field}"];
            }
        }

        return [
            'accepted' => true,
            'tenant_id' => $tenant['id'],
            'event' => 'attendance.log.created',
            'log' => [
                'employee_code' => $payload['employee_code'],
                'device_serial' => $payload['device_serial'],
                'punch_time' => $payload['punch_time'],
                'punch_type' => $payload['punch_type'],
                'mode' => $payload['mode'] ?? 'fingerprint',
            ],
            'next_jobs' => ['calculate_daily_attendance', 'broadcast_live_dashboard'],
        ];
    }
}
