<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Http\Controllers;

use Illuminate\Http\Request;
use Maestro\Core\Http\Controllers\Controller;
use Maestro\Core\Modules\HourRegistration\Services\HourCsvExportService;
use Symfony\Component\HttpFoundation\StreamedResponse;

final class HourExportController extends Controller
{
    /**
     * @return StreamedResponse
     */
    public function csv(Request $request, HourCsvExportService $svc)
    {
        $validated = validator($request->query(), [
            'from' => ['nullable', 'date'],
            'to' => ['nullable', 'date'],
            'customer_id' => ['nullable', 'integer', 'exists:customers,id'],
            'hour_project_id' => ['nullable', 'integer', 'exists:hour_projects,id'],
            'employee_ids' => ['sometimes', 'array'],
            'employee_ids.*' => ['integer', 'exists:users,id'],
        ])->validated();

        return $svc->streamCsv($request->user(), [
            'from' => $validated['from'] ?? null,
            'to' => $validated['to'] ?? null,
            'customer_id' => isset($validated['customer_id']) ? (int) $validated['customer_id'] : null,
            'hour_project_id' => isset($validated['hour_project_id']) ? (int) $validated['hour_project_id'] : null,
            'employee_ids' => $validated['employee_ids'] ?? [],
        ]);
    }
}
