<x-layouts.app title="Account Details | {{ config('app.name') }}" activePage="brokerage">
<div class="max-w-4xl mx-auto space-y-6">
    <div class="flex items-center space-x-2 text-gray-600 mb-6 text-sm">
        <a href="{{ route('brokerage') }}" class="hover:underline">Investment & Brokerage</a>
        <span>/</span>
        <span class="font-semibold underline">{{ strtoupper($account->account_type->value) }}</span>
    </div>

    <div class="flex flex-col sm:flex-row justify-between items-start gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-800 uppercase">{{ strtoupper($account->account_type->value) }} Account</h1>
            <p class="text-xs text-gray-500 mt-1">Account: {{ $account->account_number }}</p>
        </div>
        <div class="text-right">
            <p class="text-2xl font-bold text-gray-800">${{ number_format($account->balance, 2) }}</p>
            <p class="text-[10px] text-gray-500 uppercase font-semibold">Available Balance</p>
        </div>
    </div>

    @if($account->holdings->count())
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <div class="bg-white border border-gray-200 rounded-lg shadow-sm p-6">
            <h3 class="font-bold text-gray-800 mb-4">Holdings Allocation</h3>
            <div style="height: 250px;"><canvas id="holdingsPie"></canvas></div>
        </div>
        <div class="bg-white border border-gray-200 rounded-lg shadow-sm p-6">
            <h3 class="font-bold text-gray-800 mb-4">Performance (P/L)</h3>
            <div style="height: 250px;"><canvas id="plChart"></canvas></div>
        </div>
    </div>

    <div class="bg-white border border-gray-300 rounded shadow-sm overflow-x-auto">
        <div class="px-4 py-3 border-b border-gray-100">
            <h3 class="font-bold text-gray-800">Holdings</h3>
        </div>
        <table class="w-full text-left text-sm min-w-[600px]">
            <thead class="bg-gray-50 text-gray-500 text-xs uppercase">
                <tr>
                    <th class="px-4 py-3">Name</th>
                    <th class="px-4 py-3">Type</th>
                    <th class="px-4 py-3 text-right">Qty</th>
                    <th class="px-4 py-3 text-right">Avg Cost</th>
                    <th class="px-4 py-3 text-right">Current</th>
                    <th class="px-4 py-3 text-right">P/L</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @foreach($account->holdings as $h)
                <tr>
                    <td class="px-4 py-3 font-medium text-gray-800">{{ $h->name }}</td>
                    <td class="px-4 py-3 text-gray-500">{{ $h->type->label() }}</td>
                    <td class="px-4 py-3 text-right text-gray-700">{{ number_format($h->quantity, 4) }}</td>
                    <td class="px-4 py-3 text-right text-gray-700">${{ number_format($h->purchase_price, 2) }}</td>
                    <td class="px-4 py-3 text-right text-gray-700">${{ number_format($h->current_price, 2) }}</td>
                    <td class="px-4 py-3 text-right font-semibold {{ $h->profit_loss >= 0 ? 'text-green-600' : 'text-red-600' }}">{{ $h->profit_loss >= 0 ? '+' : '' }}${{ number_format($h->profit_loss, 2) }}</td>
                </tr>
                @endforeach
            </tbody>
            <tfoot class="bg-gray-50">
                <tr>
                    <td colspan="5" class="px-4 py-3 font-bold text-gray-800 text-right">Total P/L</td>
                    @php $totalPL = $account->holdings->sum('profit_loss'); @endphp
                    <td class="px-4 py-3 text-right font-bold {{ $totalPL >= 0 ? 'text-green-600' : 'text-red-600' }}">{{ $totalPL >= 0 ? '+' : '' }}${{ number_format($totalPL, 2) }}</td>
                </tr>
            </tfoot>
        </table>
    </div>
    @else
    <div class="bg-white border border-gray-300 rounded shadow-sm p-8 text-center text-gray-500">
        <p class="mb-2">No holdings in this account yet.</p>
        <p class="text-xs">Transfer funds to this account and your portfolio manager will invest on your behalf.</p>
    </div>
    @endif

    @if($transactions->count())
    <div>
        <h3 class="text-lg font-bold text-gray-800 mb-4">Transaction History</h3>
        <div class="bg-white border border-gray-300 rounded shadow-sm overflow-x-auto">
            <table class="w-full text-left text-sm min-w-[500px]">
                <thead class="bg-gray-50 text-gray-500 text-xs uppercase">
                    <tr>
                        <th class="px-4 py-3">Date</th>
                        <th class="px-4 py-3">Description</th>
                        <th class="px-4 py-3 text-right">Amount</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @foreach($transactions as $tx)
                    <tr>
                        <td class="px-4 py-3 text-gray-500">{{ $tx->created_at->format('M d, Y') }}</td>
                        <td class="px-4 py-3 text-gray-800">{{ $tx->description }}</td>
                        <td class="px-4 py-3 text-right {{ $tx->type === \App\Enums\TransactionType::Credit ? 'text-green-600' : 'text-red-600' }}">
                            {{ $tx->type === \App\Enums\TransactionType::Credit ? '+' : '-' }}${{ number_format($tx->amount, 2) }}
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <div class="mt-4">{{ $transactions->links() }}</div>
    </div>
    @endif
</div>

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
<script>
@if($account->holdings->count())
new Chart(document.getElementById('holdingsPie'), {
    type: 'doughnut',
    data: {
        labels: {!! json_encode($account->holdings->pluck('name')->values()) !!},
        datasets: [{
            data: {!! json_encode($account->holdings->map(fn($h) => (float)($h->quantity * $h->current_price))->values()) !!},
            backgroundColor: ['#1d4ed8','#059669','#d97706','#7c3aed','#dc2626','#0891b2','#4338ca','#65a30d'],
            borderWidth: 0
        }]
    },
    options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom', labels: { font: { size: 10 } } } } }
});

new Chart(document.getElementById('plChart'), {
    type: 'bar',
    data: {
        labels: {!! json_encode($account->holdings->pluck('name')->values()) !!},
        datasets: [{
            label: 'P/L ($)',
            data: {!! json_encode($account->holdings->pluck('profit_loss')->map(fn($v) => (float)$v)->values()) !!},
            backgroundColor: {!! json_encode($account->holdings->pluck('profit_loss')->map(fn($v) => $v >= 0 ? 'rgba(5,150,105,0.7)' : 'rgba(220,38,38,0.7)')->values()) !!},
            borderRadius: 4
        }]
    },
    options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { y: { beginAtZero: true } } }
});
@endif
</script>
@endpush
</x-layouts.app>
