@extends('layouts.app')
@section('title', 'Trial scorecard')

@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}
    .kpis{display:grid;grid-template-columns:repeat(4,1fr);gap:12px}
    .kpi{background:#f8fafc;border:1px solid #eef2f7;border-radius:10px;padding:14px;text-align:center}
    .kpi .v{font-size:24px;font-weight:800}.kpi .l{color:#64748b;font-size:12px;margin-top:2px}
    .flags{display:flex;gap:10px;flex-wrap:wrap;margin-top:8px}
    .flag{padding:6px 12px;border-radius:999px;font-size:13px;font-weight:600}
    .on{background:#dcfce7;color:#166534}.off{background:#f1f5f9;color:#64748b}
    table.ins{width:100%;border-collapse:collapse;font-size:13px}
    table.ins th,table.ins td{padding:7px 10px;border-bottom:1px solid #eef2f7;text-align:left}
    table.ins th{color:#64748b;font-weight:600;font-size:11px;text-transform:uppercase}
    .cols{display:grid;grid-template-columns:1fr 1fr;gap:18px}
    .tag{display:inline-block;background:#eef2f7;border-radius:6px;padding:2px 7px;margin:2px;font-size:12px}
    @media(max-width:800px){.kpis{grid-template-columns:repeat(2,1fr)}.cols{grid-template-columns:1fr}}
</style>

<section class="content-header">
    <h1 class="tw-text-xl md:tw-text-3xl tw-font-bold">{{ $business->name }} — trial scorecard</h1>
    <a href="{{ route('activity-insights.index') }}">← back to insights</a>
</section>
<section class="content">

    <div class="gt-card">
        <div class="kpis">
            <div class="kpi"><div class="v">{{ number_format($total) }}</div><div class="l">Total actions</div></div>
            <div class="kpi"><div class="v">{{ number_format($logins) }}</div><div class="l">Logins / sessions</div></div>
            <div class="kpi"><div class="v">{{ $coverage->count() }}</div><div class="l">Features touched</div></div>
            <div class="kpi"><div class="v">{{ $lastAt ? \Carbon\Carbon::parse($lastAt)->diffForHumans(null, true) : '—' }}</div><div class="l">Since last active</div></div>
        </div>
        <div class="flags">
            <span class="flag {{ $waConfigured ? 'on' : 'off' }}">WhatsApp {{ $waConfigured ? 'connected' : 'skipped' }}</span>
            <span class="flag {{ $groqConfigured ? 'on' : 'off' }}">Groq / AI {{ $groqConfigured ? 'configured' : 'skipped' }}</span>
            <span class="flag off">First appointment: {{ $firstAppt ? \Carbon\Carbon::parse($firstAppt)->format('d M H:i') : '—' }}</span>
            <span class="flag off">Demo: {{ $business->demo_status ?: 'active' }} · started {{ $business->demo_created_at ? \Carbon\Carbon::parse($business->demo_created_at)->format('d M H:i') : '—' }}</span>
        </div>
    </div>

    <div class="cols">
        <div class="gt-card">
            <p class="gt-h">Feature coverage</p>
            @forelse ($coverage as $model => $c)
                <span class="tag">{{ class_basename($model) }} ({{ $c }})</span>
            @empty
                <p>No activity recorded.</p>
            @endforelse
        </div>

        <div class="gt-card">
            <p class="gt-h">Mistakes ({{ $incByType->sum() }})</p>
            @forelse ($incByType as $type => $c)
                <span class="tag">{{ $type }}: <strong>{{ $c }}</strong></span>
            @empty
                <p>No incidents — clean run 🎉</p>
            @endforelse
        </div>
    </div>

    <div class="gt-card">
        <p class="gt-h">Activity by user</p>
        <table class="ins">
            <thead><tr><th>User</th><th>Actions</th><th>Last active</th></tr></thead>
            <tbody>
            @foreach ($perUser as $u)
                <tr>
                    <td>{{ trim($u->name) ?: ('User #' . $u->user_id) }}</td>
                    <td>{{ number_format($u->c) }}</td>
                    <td>{{ $u->last_at ? \Carbon\Carbon::parse($u->last_at)->diffForHumans() : '—' }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>

    @if ($recentIncidents->count())
    <div class="gt-card">
        <p class="gt-h">Recent incidents</p>
        <table class="ins">
            <thead><tr><th>When</th><th>Type</th><th>Code</th><th>Route</th><th>Message</th></tr></thead>
            <tbody>
            @foreach ($recentIncidents as $inc)
                <tr>
                    <td>{{ $inc->created_at ? \Carbon\Carbon::parse($inc->created_at)->format('d M H:i') : '—' }}</td>
                    <td>{{ $inc->type }}</td>
                    <td>{{ $inc->status_code }}</td>
                    <td><small>{{ $inc->method }} {{ \Illuminate\Support\Str::limit($inc->route, 40) }}</small></td>
                    <td><small>{{ \Illuminate\Support\Str::limit($inc->message ?: $inc->exception_class, 60) }}</small></td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
    @endif

</section>
@endsection
