<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AttendanceLog;
use App\Services\Attendance\AttendanceEngine;
use Illuminate\Http\Request;

class AttendanceController extends Controller
{
    public function sync(Request $request)
    {
        $data = $request->validate([
            'device_id' => ['required', 'integer'],
            'employee_id' => ['required', 'integer'],
            'punch_type' => ['required', 'string'],
            'verify_mode' => ['required', 'string'],
            'punched_at' => ['required', 'date'],
            'raw_payload' => ['array'],
        ]);

        $data['tenant_id'] = $request->user()->tenant_id;
        $data['source'] = 'api';

        return AttendanceLog::create($data);
    }

    public function calculate(Request $request, AttendanceEngine $engine)
    {
        $request->validate(['date' => ['required', 'date']]);

        return $engine->calculateDailySummary($request->user()->tenant_id, now()->parse($request->date));
    }
}
