<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">Manual + API Ready</p>
                <h2 class="text-2xl font-bold text-slate-950 dark:text-white">Trading Accounts</h2>
            </div>
            <a href="{{ route('trading-accounts.create') }}" class="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">
                Add Account
            </a>
        </div>
    </x-slot>

    <div class="grid gap-4 md:grid-cols-4">
        @foreach ([
            ['Total Accounts', $totals['accounts']],
            ['Manual Mode', $totals['manual']],
            ['API Connected', $totals['api_connected']],
            ['Total Balance', number_format($totals['balance'], 2)],
        ] as [$label, $value])
            <div class="rounded-lg border border-blue-100 bg-white p-5 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
                <p class="text-sm font-semibold text-slate-500 dark:text-slate-400">{{ $label }}</p>
                <p class="mt-2 text-2xl font-bold text-slate-950 dark:text-white">{{ $value }}</p>
            </div>
        @endforeach
    </div>

    <div class="mt-6 overflow-hidden rounded-lg border border-blue-100 bg-white shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
        <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">Account</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Mode</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Balance</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Equity</th>
                        <th class="px-4 py-3 text-left text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Last Sync</th>
                        <th class="px-4 py-3 text-right text-xs font-bold uppercase text-blue-700 dark:text-blue-300">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-100 dark:divide-blue-900/50">
                    @forelse ($accounts as $account)
                        <tr>
                            <td class="px-4 py-4">
                                <p class="font-semibold text-slate-950 dark:text-white">{{ $account->name }}</p>
                                <p class="text-sm text-slate-500 dark:text-slate-400">{{ $account->broker_name }} {{ $account->account_number ? ' / '.$account->account_number : '' }}</p>
                            </td>
                            <td class="px-4 py-4">
                                <span class="rounded-full px-3 py-1 text-xs font-bold {{ $account->api_connected ? 'bg-blue-100 text-blue-700 dark:bg-blue-950 dark:text-blue-200' : 'bg-emerald-100 text-emerald-700 dark:bg-emerald-950 dark:text-emerald-200' }}">
                                    {{ $account->api_connected ? 'API' : 'Manual' }}
                                </span>
                            </td>
                            <td class="px-4 py-4 text-sm font-semibold text-slate-700 dark:text-slate-200">{{ $account->currency }} {{ number_format($account->balance, 2) }}</td>
                            <td class="px-4 py-4 text-sm font-semibold text-slate-700 dark:text-slate-200">{{ $account->currency }} {{ number_format($account->equity, 2) }}</td>
                            <td class="px-4 py-4 text-sm text-slate-500 dark:text-slate-400">{{ $account->last_synced_at?->diffForHumans() ?? 'Never' }}</td>
                            <td class="px-4 py-4 text-right">
                                <div class="flex justify-end gap-2">
                                    <a href="{{ route('trading-accounts.show', $account) }}" class="text-sm font-bold text-blue-700 hover:text-blue-900 dark:text-blue-300">View</a>
                                    <a href="{{ route('trading-accounts.edit', $account) }}" class="text-sm font-bold text-slate-600 hover:text-slate-900 dark:text-slate-300">Edit</a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-10 text-center text-slate-500 dark:text-slate-400">No trading accounts yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-6">
        {{ $accounts->links() }}
    </div>
</x-app-layout>
