@php
    $money = fn ($minor) => config('care.billing.currency').' '.number_format($minor / 100, 2);
    $formatValue = function ($key, $value) use ($money) {
        if (str_ends_with($key, '_minor')) return $money((int) $value);
        return $value;
    };
@endphp
<x-app-layout :title="$label">
    <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-xl font-semibold text-slate-900">{{ $label }}</h1>
            <p class="text-sm text-slate-500">{{ $from }} to {{ $to }}</p>
        </div>
        @if ($canExport)
            <a href="{{ route('care.reports.export', ['type' => $type, 'from' => $from, 'to' => $to, 'branch_id' => $branchId]) }}" class="btn-primary">Export CSV</a>
        @endif
    </div>
    <form method="GET" class="mt-4 flex flex-wrap gap-3 rounded-2xl border border-slate-200 bg-white p-4">
        <input type="date" name="from" value="{{ $from }}" class="rounded-lg border-slate-300 text-sm">
        <input type="date" name="to" value="{{ $to }}" class="rounded-lg border-slate-300 text-sm">
        <select name="branch_id" class="rounded-lg border-slate-300 text-sm">
            <option value="">All branches</option>
            @foreach ($branches as $branch)
                <option value="{{ $branch->id }}" @selected($branchId == $branch->id)>{{ $branch->name }}</option>
            @endforeach
        </select>
        <button type="submit" class="btn-primary">Apply</button>
    </form>
    <section class="mt-6 space-y-6">
        @if ($type === 'clinical' && isset($data['diagnoses']))
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <table class="min-w-full text-sm">
                    <thead><tr><th class="py-2 text-left">Diagnosis</th><th class="py-2 text-right">Count</th></tr></thead>
                    <tbody>
                        @forelse ($data['diagnoses'] as $row)
                            <tr class="border-t border-slate-50"><td class="py-2">{{ $row->description }}</td><td class="py-2 text-right">{{ $row->total }}</td></tr>
                        @empty
                            <tr><td colspan="2" class="py-4 text-slate-500">No diagnoses in period.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        @elseif ($type === 'assessments')
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Summary</h2>
                <dl class="mt-4 grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
                    @foreach ($data['summary'] ?? [] as $key => $value)
                        <div class="rounded-xl border border-slate-100 bg-slate-50 p-4">
                            <dt class="text-xs uppercase text-slate-500">{{ str_replace('_', ' ', $key) }}</dt>
                            <dd class="mt-1 text-2xl font-semibold text-slate-900">{{ $value }}</dd>
                        </div>
                    @endforeach
                </dl>
            </div>
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">By clinical form</h2>
                <table class="mt-4 min-w-full text-sm">
                    <thead>
                        <tr>
                            <th class="py-2 text-left">Template</th>
                            <th class="py-2 text-right">Started</th>
                            <th class="py-2 text-right">Completed</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($data['by_template'] ?? [] as $row)
                            <tr class="border-t border-slate-50">
                                <td class="py-2">{{ $row->template_name }} <span class="text-slate-400">({{ $row->template_code }})</span></td>
                                <td class="py-2 text-right">{{ $row->total }}</td>
                                <td class="py-2 text-right">{{ $row->completed }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="py-4 text-slate-500">No assessments in period.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Pathway activations</h2>
                <table class="mt-4 min-w-full text-sm">
                    <thead>
                        <tr>
                            <th class="py-2 text-left">Pathway</th>
                            <th class="py-2 text-right">Activations</th>
                            <th class="py-2 text-right">Still active</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($data['by_pathway'] ?? [] as $row)
                            <tr class="border-t border-slate-50">
                                <td class="py-2">{{ $row->pathway_name }}</td>
                                <td class="py-2 text-right">{{ $row->total }}</td>
                                <td class="py-2 text-right">{{ $row->still_active }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="py-4 text-slate-500">No pathway activations in period.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Average scores</h2>
                <table class="mt-4 min-w-full text-sm">
                    <thead>
                        <tr>
                            <th class="py-2 text-left">Instrument</th>
                            <th class="py-2 text-right">Completions</th>
                            <th class="py-2 text-right">Avg total</th>
                            <th class="py-2 text-right">Avg max</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($data['score_averages'] ?? [] as $row)
                            <tr class="border-t border-slate-50">
                                <td class="py-2 font-mono text-xs">{{ $row->template_code }}</td>
                                <td class="py-2 text-right">{{ $row->completions }}</td>
                                <td class="py-2 text-right">{{ $row->avg_total !== null ? round((float) $row->avg_total, 2) : '—' }}</td>
                                <td class="py-2 text-right">{{ $row->avg_max !== null ? round((float) $row->avg_max, 2) : '—' }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="py-4 text-slate-500">No scored completions in period.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        @else
            <div class="rounded-2xl border border-slate-200 bg-white p-6">
                <dl class="grid gap-4 sm:grid-cols-2">
                    @foreach ($data as $key => $value)
                        <div class="rounded-xl border border-slate-100 bg-slate-50 p-4">
                            <dt class="text-xs uppercase text-slate-500">{{ str_replace('_', ' ', $key) }}</dt>
                            <dd class="mt-1 text-2xl font-semibold text-slate-900">{{ $formatValue($key, $value) }}</dd>
                        </div>
                    @endforeach
                </dl>
            </div>
        @endif
    </section>
</x-app-layout>
