@extends('admin.layout')
@section('title', 'Reports')

@section('content')

<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">

    {{-- Daily Revenue --}}
    <div class="card">
        <div class="p-5 border-b border-white/5">
            <h3 class="font-semibold text-white text-sm">Daily Revenue (last 30 days)</h3>
        </div>
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Trades</th>
                        <th>Volume</th>
                        <th>House Profit</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($daily_revenue as $row)
                    <tr>
                        <td class="text-xs font-mono">{{ $row->date }}</td>
                        <td>{{ number_format($row->total_trades) }}</td>
                        <td class="font-mono">${{ number_format($row->volume, 2) }}</td>
                        <td class="font-mono {{ $row->house_profit >= 0 ? 'text-green-400' : 'text-red-400' }}">
                            {{ $row->house_profit >= 0 ? '+' : '' }}${{ number_format($row->house_profit, 2) }}
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="4" class="text-center text-gray-500 py-6">No data yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- New Registrations --}}
    <div class="card">
        <div class="p-5 border-b border-white/5">
            <h3 class="font-semibold text-white text-sm">New Registrations (last 30 days)</h3>
        </div>
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>New Users</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($registrations as $row)
                    <tr>
                        <td class="text-xs font-mono">{{ $row->date }}</td>
                        <td class="text-green-400 font-bold">{{ $row->count }}</td>
                    </tr>
                    @empty
                    <tr><td colspan="2" class="text-center text-gray-500 py-6">No data yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">

    {{-- Top Users by Volume --}}
    <div class="card">
        <div class="p-5 border-b border-white/5">
            <h3 class="font-semibold text-white text-sm">Top Users by Trade Volume</h3>
        </div>
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User</th>
                        <th>Trades</th>
                        <th>Volume</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($top_users as $i => $user)
                    <tr>
                        <td class="text-gray-500 text-xs">{{ $i + 1 }}</td>
                        <td>
                            <a href="/ponir/users/{{ $user->id }}" class="text-white hover:text-green-400">{{ $user->name }}</a>
                            <div class="text-xs text-gray-500">{{ $user->email }}</div>
                        </td>
                        <td>{{ number_format($user->trade_count) }}</td>
                        <td class="font-mono text-cyan-400">${{ number_format($user->total_volume, 2) }}</td>
                    </tr>
                    @empty
                    <tr><td colspan="4" class="text-center text-gray-500 py-6">No data yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- Asset Stats --}}
    <div class="card">
        <div class="p-5 border-b border-white/5">
            <h3 class="font-semibold text-white text-sm">Win/Loss by Asset</h3>
        </div>
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th>Asset</th>
                        <th>Total</th>
                        <th>Won</th>
                        <th>Lost</th>
                        <th>Win%</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($asset_stats as $row)
                    @php $winPct = $row->total > 0 ? round($row->wins / $row->total * 100) : 0; @endphp
                    <tr>
                        <td class="font-mono">{{ $row->asset_symbol }}</td>
                        <td>{{ number_format($row->total) }}</td>
                        <td class="text-green-400">{{ number_format($row->wins) }}</td>
                        <td class="text-red-400">{{ number_format($row->losses) }}</td>
                        <td>
                            <div class="flex items-center gap-2">
                                <div class="w-20 h-1.5 rounded-full" style="background:rgba(255,255,255,.05);">
                                    <div class="h-1.5 rounded-full {{ $winPct >= 50 ? 'bg-green-500' : 'bg-red-500' }}" style="width:{{ $winPct }}%"></div>
                                </div>
                                <span class="text-xs {{ $winPct >= 50 ? 'text-green-400' : 'text-red-400' }}">{{ $winPct }}%</span>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="5" class="text-center text-gray-500 py-6">No data yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@endsection
