<?php

namespace App\Http\Controllers;

use App\Services\FmpEconomicCalendarService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ForexCalendarController extends Controller
{
    public function __invoke(Request $request, FmpEconomicCalendarService $fmpCalendar): View
    {
        $filters = $request->validate([
            'date' => ['nullable', 'date'],
            'country' => ['nullable', 'string', 'max:100'],
            'currency' => ['nullable', 'string', 'size:3'],
        ]);

        $filters['date'] = $filters['date'] ?? now()->toDateString();

        if (isset($filters['currency'])) {
            $filters['currency'] = strtoupper($filters['currency']);
        }

        $calendar = $fmpCalendar->economicCalendar($filters);

        return view('forex-calendar', [
            'events' => $calendar['events'],
            'status' => $calendar['status'],
            'message' => $calendar['message'],
            'filters' => $filters,
            'apiKeyConfigured' => $calendar['api_key_configured'],
        ]);
    }
}
