@php
    $stages = config('southend.disciplinary_stages', []);
    $stageKeys = array_keys($stages);
    $currentIndex = array_search($case->stage, $stageKeys, true);
@endphp

<x-panel title="Case Workflow">
    <div style="display:flex; gap:8px; flex-wrap:wrap; margin-bottom:20px;">
        @foreach ($stageKeys as $index => $stageKey)
            @if ($stageKey === 'dismissed') @continue @endif
            <div style="padding:8px 12px; border-radius:8px; font-size:13px; {{ $case->stage === $stageKey ? 'background:var(--primary); color:#fff;' : ($currentIndex !== false && $index < $currentIndex ? 'background:var(--green-bg); color:var(--green);' : 'background:var(--bg2); color:var(--text2);') }}">
                {{ $stages[$stageKey] ?? $stageKey }}
            </div>
        @endforeach
    </div>

    <dl class="detail-grid">
        <dt>Case Number</dt><dd>{{ $case->case_number }}</dd>
        <dt>Stage</dt><dd><x-badge variant="blue">{{ $casesService->stageLabel($case->stage) }}</x-badge></dd>
        <dt>Subject Student</dt><dd>{{ $case->subjectStudent?->full_name }}</dd>
        <dt>Reported By</dt><dd>{{ $casesService->reporterDisplayName($case, auth()->user()) }}</dd>
        <dt>Class</dt><dd>{{ $case->schoolClass?->name ?? '—' }}</dd>
        <dt>Class Teacher</dt><dd>{{ $case->classTeacher?->full_name ?? '—' }}</dd>
        <dt>Category</dt><dd>{{ config('southend.disciplinary_categories.'.$case->category, $case->category ?? '—') }}</dd>
        <dt>Severity</dt><dd>{{ ucfirst($case->severity) }}</dd>
        <dt>Title</dt><dd>{{ $case->title }}</dd>
        <dt>Description</dt><dd style="white-space:pre-wrap;">{{ $case->description }}</dd>
        @if ($case->outcome)
            <dt>Outcome</dt><dd><x-badge variant="red">{{ $casesService->outcomeLabel($case->outcome) }}</x-badge></dd>
            @if ($case->outcome_details)<dt>Outcome Details</dt><dd>{{ $case->outcome_details }}</dd>@endif
            @if ($case->suspension_starts_on)
                <dt>Suspension</dt><dd>{{ $case->suspension_starts_on->format('d M Y') }} — {{ $case->suspension_ends_on?->format('d M Y') ?? 'ongoing' }}</dd>
            @endif
            <dt>Parent Notified</dt><dd>{{ $case->parent_notified ? 'Yes' : 'No' }}</dd>
        @endif
    </dl>
    @if ($case->attachments->isNotEmpty())
        <h4 style="margin-top:16px;">Evidence</h4>
        <ul>@foreach ($case->attachments as $file)<li><a href="{{ asset('storage/'.$file->file_path) }}" target="_blank">{{ $file->file_name }}</a></li>@endforeach</ul>
    @endif
</x-panel>

@if (!empty($nextStages) && ! $case->isClosed())
    <x-panel title="Advance Case">
        <form method="POST" action="{{ $advanceRoute }}">
            @csrf
            <div style="display:grid; gap:16px; max-width:640px;">
                <div class="form-group">
                    <label class="form-label" for="to_stage">Move To Stage</label>
                    <select class="input" id="to_stage" name="to_stage" required>
                        @foreach ($nextStages as $stage)
                            <option value="{{ $stage }}">{{ $casesService->stageLabel($stage) }}</option>
                        @endforeach
                    </select>
                </div>
                @if ($showOutcomeForm ?? false)
                    <div class="form-group">
                        <label class="form-label" for="outcome">Outcome (required when closing)</label>
                        <select class="input" id="outcome" name="outcome">
                            <option value="">— Select outcome —</option>
                            @foreach ($outcomes ?? [] as $outcome)
                                <option value="{{ $outcome }}">{{ $casesService->outcomeLabel($outcome) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="form-label" for="outcome_details">Outcome Details</label>
                        <textarea class="input" id="outcome_details" name="outcome_details" rows="3"></textarea>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><input type="checkbox" name="parent_notified" value="1"> Parent notified</label>
                    </div>
                    <div style="display:grid; grid-template-columns:1fr 1fr; gap:16px;">
                        <div class="form-group">
                            <label class="form-label" for="suspension_starts_on">Suspension starts</label>
                            <input class="input" type="date" id="suspension_starts_on" name="suspension_starts_on">
                        </div>
                        <div class="form-group">
                            <label class="form-label" for="suspension_ends_on">Suspension ends</label>
                            <input class="input" type="date" id="suspension_ends_on" name="suspension_ends_on">
                        </div>
                    </div>
                @endif
                <div class="form-group">
                    <label class="form-label" for="notes">Notes</label>
                    <textarea class="input" id="notes" name="notes" rows="3" placeholder="Investigation notes, hearing summary, etc."></textarea>
                </div>
                <button type="submit" class="btn btn-primary">Update Case</button>
            </div>
        </form>
    </x-panel>
@endif

<x-panel title="Case History">
    @if ($case->actions->isEmpty())
        <p style="color:var(--text2);">No actions recorded yet.</p>
    @else
        <div class="table-wrap"><table><thead><tr><th>Date</th><th>Action</th><th>Stage</th><th>By</th><th>Notes</th></tr></thead><tbody>
            @foreach ($case->actions as $action)
                <tr>
                    <td>{{ $action->created_at->format('d M Y H:i') }}</td>
                    <td>{{ $action->action }}</td>
                    <td>{{ $casesService->stageLabel($action->to_stage) }}</td>
                    <td>{{ $action->performedBy?->name ?? '—' }}</td>
                    <td>{{ $action->notes ?? '—' }}</td>
                </tr>
            @endforeach
        </tbody></table></div>
    @endif
</x-panel>
