<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-semibold uppercase tracking-wide text-blue-600 dark:text-blue-300">Financial Modeling Prep</p>
                <h2 class="text-2xl font-bold text-slate-950 dark:text-white">Forex Calendar</h2>
            </div>
            <div class="rounded-lg border border-blue-100 bg-white px-4 py-2 text-sm font-semibold text-slate-600 dark:border-blue-900/50 dark:bg-slate-900 dark:text-slate-300">
                {{ $apiKeyConfigured ? 'FMP connected' : 'FMP API key not connected' }}
            </div>
        </div>
    </x-slot>

    <section class="grid gap-6 lg:grid-cols-3">
        <div class="rounded-lg border border-blue-100 bg-white p-6 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
            <p class="text-sm font-semibold text-slate-500 dark:text-slate-400">Provider</p>
            <h3 class="mt-2 text-xl font-bold text-slate-950 dark:text-white">Financial Modeling Prep API</h3>
            <p class="mt-3 text-sm leading-6 text-slate-600 dark:text-slate-300">
                Economic calendar requests are made from Laravel using the backend API key. The key is never exposed in Blade or JavaScript.
            </p>
        </div>

        <div class="rounded-lg border border-blue-100 bg-white p-6 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
            <p class="text-sm font-semibold text-slate-500 dark:text-slate-400">Status</p>
            <h3 class="mt-2 text-xl font-bold text-slate-950 dark:text-white">{{ str($status)->replace('_', ' ')->title() }}</h3>
            <p class="mt-3 text-sm leading-6 text-slate-600 dark:text-slate-300">
                {{ $message ?? 'Calendar data loaded successfully.' }}
            </p>
        </div>

        <div class="rounded-lg border border-blue-100 bg-white p-6 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
            <p class="text-sm font-semibold text-slate-500 dark:text-slate-400">Impact Guide</p>
            <div class="mt-4 flex flex-wrap gap-2">
                <span class="rounded-full bg-slate-100 px-3 py-1 text-xs font-bold text-slate-700 dark:bg-slate-800 dark:text-slate-200">Low</span>
                <span class="rounded-full bg-amber-100 px-3 py-1 text-xs font-bold text-amber-700 dark:bg-amber-950 dark:text-amber-200">Medium</span>
                <span class="rounded-full bg-red-100 px-3 py-1 text-xs font-bold text-red-700 dark:bg-red-950 dark:text-red-200">High</span>
            </div>
        </div>
    </section>

    <section class="mt-6 rounded-lg border border-blue-100 bg-white p-6 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
        <form method="GET" action="{{ route('forex-calendar') }}" class="grid gap-4 md:grid-cols-4">
            <div>
                <label for="date" class="text-sm font-semibold text-slate-700 dark:text-slate-200">Date</label>
                <input id="date" type="date" name="date" value="{{ $filters['date'] ?? now()->toDateString() }}" class="mt-1 block w-full rounded-lg border-blue-100 bg-white px-3 py-2 text-sm text-slate-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
            </div>

            <div>
                <label for="country" class="text-sm font-semibold text-slate-700 dark:text-slate-200">Country</label>
                <input id="country" name="country" value="{{ $filters['country'] ?? '' }}" placeholder="United States" class="mt-1 block w-full rounded-lg border-blue-100 bg-white px-3 py-2 text-sm text-slate-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
            </div>

            <div>
                <label for="currency" class="text-sm font-semibold text-slate-700 dark:text-slate-200">Currency</label>
                <input id="currency" name="currency" maxlength="3" value="{{ $filters['currency'] ?? '' }}" placeholder="USD" class="mt-1 block w-full rounded-lg border-blue-100 bg-white px-3 py-2 text-sm uppercase text-slate-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
            </div>

            <div class="flex items-end gap-2">
                <button type="submit" class="w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-bold text-white shadow-sm shadow-blue-600/20 transition hover:-translate-y-0.5 hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-400">
                    Filter
                </button>
                <a href="{{ route('forex-calendar') }}" class="rounded-lg border border-blue-100 px-4 py-2 text-sm font-bold text-blue-700 transition hover:-translate-y-0.5 hover:bg-blue-50 dark:border-blue-900 dark:text-blue-300 dark:hover:bg-blue-950/60">
                    Reset
                </a>
            </div>
        </form>
    </section>

    @if ($message)
        <section class="mt-6 rounded-lg border {{ $status === 'success' ? 'border-blue-100 bg-blue-50 text-blue-800 dark:border-blue-900/50 dark:bg-blue-950/30 dark:text-blue-200' : 'border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-900/60 dark:bg-amber-950/40 dark:text-amber-200' }} px-5 py-4 text-sm font-semibold">
            {{ $message }}
        </section>
    @endif

    <section class="mt-6 rounded-lg border border-blue-100 bg-white shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
        <div class="border-b border-blue-100 px-6 py-5 dark:border-blue-900/50">
            <h3 class="text-lg font-bold text-slate-950 dark:text-white">Economic Events</h3>
            <p class="mt-1 text-sm text-slate-600 dark:text-slate-400">
                Data source: Financial Modeling Prep Economic Calendar API.
            </p>
        </div>

        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-blue-100 dark:divide-blue-900/50">
                <thead class="bg-blue-50/70 dark:bg-blue-950/30">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Time</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Country</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Currency</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Event</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Impact</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Actual</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Forecast</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Previous</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-100 dark:divide-blue-900/50">
                    @forelse ($events as $event)
                        <tr>
                            <td class="whitespace-nowrap px-4 py-4 text-sm font-semibold text-slate-700 dark:text-slate-200">
                                @if ($event['datetime'])
                                    {{ \Illuminate\Support\Carbon::parse($event['datetime'])->format('M d, H:i') }}
                                @else
                                    -
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $event['country'] ?: '-' }}</td>
                            <td class="whitespace-nowrap px-4 py-4 text-sm font-bold text-slate-900 dark:text-white">{{ $event['currency'] ?: '-' }}</td>
                            <td class="min-w-64 px-4 py-4 text-sm font-semibold text-slate-900 dark:text-white">{{ $event['event'] ?: '-' }}</td>
                            <td class="whitespace-nowrap px-4 py-4">
                                @php
                                    $impactClass = match ($event['impact']) {
                                        'High' => 'bg-red-100 text-red-700 dark:bg-red-950 dark:text-red-200',
                                        'Medium' => 'bg-amber-100 text-amber-700 dark:bg-amber-950 dark:text-amber-200',
                                        'Low' => 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200',
                                        default => 'bg-blue-100 text-blue-700 dark:bg-blue-950 dark:text-blue-200',
                                    };
                                @endphp
                                <span class="rounded-full px-3 py-1 text-xs font-bold {{ $impactClass }}">{{ $event['impact'] }}</span>
                            </td>
                            <td class="whitespace-nowrap px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ filled($event['actual']) ? $event['actual'] : '-' }}</td>
                            <td class="whitespace-nowrap px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ filled($event['forecast']) ? $event['forecast'] : '-' }}</td>
                            <td class="whitespace-nowrap px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ filled($event['previous']) ? $event['previous'] : '-' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-4 py-10 text-center">
                                <p class="font-semibold text-slate-700 dark:text-slate-200">
                                    {{ $status === 'missing_key' ? 'FMP API key is not connected yet.' : 'No economic events found for this filter.' }}
                                </p>
                                <p class="mt-2 text-sm text-slate-500 dark:text-slate-400">
                                    Add a valid Financial Modeling Prep API key, or change your filter values and try again.
                                </p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </section>
</x-app-layout>
