@extends('layouts.app')
@section('title', 'TPS Insights')

@section('content')
<style>
    .gt-card{background:#fff;border:1px solid #e2e8f0;border-radius:12px;padding:16px;margin-bottom:18px}
    .gt-h{font-size:16px;font-weight:700;margin:0 0 12px}
    .heat{display:grid;grid-template-columns:90px repeat(24,1fr);gap:2px;font-size:10px;overflow-x:auto}
    .heat .cell{height:16px;border-radius:2px;background:#eef2f7}
    .heat .lbl{color:#64748b;font-size:10px;line-height:16px;white-space:nowrap}
    .heat .hh{color:#94a3b8;text-align:center}
    table.ins{width:100%;border-collapse:collapse;font-size:13px}
    table.ins th,table.ins td{padding:8px 10px;border-bottom:1px solid #eef2f7;text-align:left}
    table.ins th{color:#64748b;font-weight:600;text-transform:uppercase;font-size:11px;letter-spacing:.04em}
    .pill{padding:2px 8px;border-radius:999px;font-size:11px;font-weight:600}
    .p-active{background:#dcfce7;color:#166534}.p-expired{background:#fef9c3;color:#854d0e}.p-purged{background:#fee2e2;color:#b91c1c}
    .badnum{color:#b91c1c;font-weight:700}
</style>

<section class="content-header"><h1 class="tw-text-xl md:tw-text-3xl tw-font-bold">TPS Insights — trials &amp; activity</h1></section>
<section class="content">

    {{-- Heat-map --}}
    <div class="gt-card">
        <p class="gt-h">Activity heat-map — last 14 days (rows = day, columns = hour)</p>
        <div class="heat">
            <div class="lbl"></div>
            @for ($h = 0; $h < 24; $h++)<div class="hh">{{ $h }}</div>@endfor
            @foreach ($heat['days'] as $d)
                <div class="lbl">{{ \Carbon\Carbon::parse($d)->format('D d M') }}</div>
                @for ($h = 0; $h < 24; $h++)
                    @php
                        $c = $heat['grid'][$d][$h] ?? 0;
                        $intensity = $c ? max(0.12, min(1, $c / $heat['max'])) : 0;
                        $bg = $c ? 'rgba(13,148,136,' . number_format($intensity, 2) . ')' : '#eef2f7';
                    @endphp
                    <div class="cell" style="background:{{ $bg }}" title="{{ $d }} {{ $h }}:00 — {{ $c }} actions"></div>
                @endfor
            @endforeach
        </div>
    </div>

    {{-- Demo tenants --}}
    <div class="gt-card">
        <p class="gt-h">Demo tenants ({{ count($tenants) }})</p>
        <table class="ins">
            <thead><tr>
                <th>Clinic</th><th>Status</th><th>Started</th><th>Expires</th>
                <th>Activity</th><th>Mistakes</th><th>Last active</th><th></th>
            </tr></thead>
            <tbody>
            @forelse ($tenants as $t)
                <tr>
                    <td><strong>{{ $t['name'] }}</strong> <small>#{{ $t['id'] }}</small></td>
                    <td><span class="pill p-{{ $t['status'] ?: 'active' }}">{{ $t['status'] ?: 'active' }}</span></td>
                    <td>{{ $t['created_at'] ? \Carbon\Carbon::parse($t['created_at'])->format('d M H:i') : '—' }}</td>
                    <td>{{ $t['expires_at'] ? \Carbon\Carbon::parse($t['expires_at'])->format('d M H:i') : '—' }}</td>
                    <td>{{ number_format($t['activity']) }}</td>
                    <td class="{{ $t['incidents'] ? 'badnum' : '' }}">{{ $t['incidents'] }}</td>
                    <td>{{ $t['last_at'] ? \Carbon\Carbon::parse($t['last_at'])->diffForHumans() : '—' }}</td>
                    <td><a href="{{ route('activity-insights.tenant', $t['id']) }}">Scorecard →</a></td>
                </tr>
            @empty
                <tr><td colspan="8">No demo tenants yet.</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>

    {{-- Top users --}}
    <div class="gt-card">
        <p class="gt-h">Most active users</p>
        <table class="ins">
            <thead><tr><th>User</th><th>Business</th><th>Actions</th></tr></thead>
            <tbody>
            @foreach ($topUsers as $u)
                <tr>
                    <td>{{ trim($u->name) ?: ('User #' . $u->user_id) }}</td>
                    <td>#{{ $u->business_id }}</td>
                    <td>{{ number_format($u->c) }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>

</section>
@endsection
