<?php

namespace App\Components\Ha;

use App\Models\Claim\Claim;
use Livewire\Attributes\Lazy;
use Livewire\Component;

/**
 * Dashboard column: the HA claims assigned to the currently logged-in
 * handler, grouped by status. A claim is "assigned" to a handler when the
 * claim's client has a client_handlers row pointing at that user
 * (see App\Models\Client\ClientHandler).
 */
#[Lazy]
class HandlerClaims extends Component
{
    public function render()
    {
        $handlerId = auth()->id();

        $claims = Claim::query()
            ->whereHas('client.handlers', fn ($q) => $q->where('user_id', $handlerId))
            ->with(['client', 'status'])
            ->get();

        // Group by status display_name, keeping the status colour for the badge.
        $grouped = $claims
            ->sortBy(fn ($claim) => $claim->status?->sort_order ?? 999)
            ->groupBy(fn ($claim) => $claim->status?->display_name ?? 'No status');

        return view('components.ha.handler-claims', [
            'grouped' => $grouped,
            'total'   => $claims->count(),
        ]);
    }

    public function placeholder()
    {
        return <<<'HTML'
        <div class="placeholder-glow">
            <span class="placeholder col-12 d-block mb-1"></span>
            <span class="placeholder col-9 d-block mb-1"></span>
            <span class="placeholder col-6 d-block"></span>
        </div>
        HTML;
    }
}
