<?php

namespace App\Components\PensionCase;

use App\Models\Claim\Status;
use App\Models\PensionCase\PensionCase;
use Livewire\Attributes\Lazy;
use Livewire\Component;
use Livewire\WithPagination;

#[Lazy]
class Index extends Component {
    use WithPagination;

    public string $search    = '';
    public string $openClosed = 'all';   // all | open | closed
    public string $claimType = '';       // '' | bank | fscs | fos
    public ?int   $statusId  = null;
    public string $vulnerable = '';
    public int    $perPage   = 25;

    public function updatingSearch(): void    { $this->resetPage(); }
    public function updatingClaimType(): void { $this->statusId = null; $this->resetPage(); }
    public function updatingStatusId(): void  { $this->resetPage(); }
    public function updatingOpenClosed(): void { $this->resetPage(); }

    public function render() {
        $query = PensionCase::with(['client', 'status', 'leadSource'])->orderByDesc('created_at');

        if ($this->search) {
            $term = '%' . $this->search . '%';
            $tokens = preg_split('/\s+/', $term, -1, PREG_SPLIT_NO_EMPTY);

            if (count($tokens) > 1) {
                $query->orWhereHas('client', function ($q) use ($tokens, $term) {
                    foreach ($tokens as $token) {
                        $q->where(function ($inner) use ($token) {
                            $inner->where('name_first', 'like', '%' . $token . '%')
                                ->orWhere('name_middle', 'like', '%' . $token . '%')
                                ->orWhere('name_last', 'like', '%' . $token . '%')
                                ->orWhere('name_preferred', 'like', '%' . $token . '%');
                        });
                    }
                });
            } else {
                $query->orWhereHas('client', function ($que) use ($term) {
                    $que->where('name_first', 'like', '%' . $term . '%')
                        ->orWhere('name_middle', 'like', '%' . $term . '%')
                        ->orWhere('name_last', 'like', '%' . $term . '%')
                        ->orWhere('name_preferred', 'like', '%' . $term . '%');
                });
            }

            $query->orWhere('sf_case_id', 'like', $term)
                  ->orWhere('case_number', 'like', $term)
                  ->orWhere('claim_subject', 'like', $term)
                  ->orWhere('national_insurance', 'like', $term)
                  ->orWhere('investment_library', 'like', $term)
                  ->orWhere('cmc_client_reference', 'like', $term)
                  ->orWhere('fscs_reference', 'like', $term)
                  ->orWhereHas('leadSource', function ($q) use ($term) {
                      $q->where('display_name', 'like', $term);
                  });
        }

        if ($this->openClosed === 'open') {
            $query->open();
        } elseif ($this->openClosed === 'closed') {
            $query->closed();
        }

        if ($this->claimType) {
            $query->where('claim_type', $this->claimType);
        }

        if ($this->statusId) {
            $query->where('status_id', $this->statusId);
        }

        if ($this->vulnerable === '1') {
            $query->vulnerable();
        }

        // Statuses for the sidebar dropdown — scoped to selected claim type if set
        $statuses = $this->claimType
            ? Status::forType('pension-' . $this->claimType)->get()
            : collect();

        return view('components.pension-case.index', [
            'cases'    => $query->paginate($this->perPage),
            'statuses' => $statuses,
        ]);
    }

    public function placeholder() {
        return <<<HTML
<div>
    <div class="placeholder-glow mb-3">
        <span class="placeholder col-4 placeholder-lg"></span>
    </div>
    <div class="card shadow-sm">
        <div class="card-body">
            <div class="placeholder-glow">
                <span class="placeholder col-12 mb-2"></span>
                <span class="placeholder col-12 mb-2"></span>
                <span class="placeholder col-10 mb-2"></span>
            </div>
        </div>
    </div>
</div>
HTML;
    }
}
