<?php

namespace App\Components\Institution;

use App\Models\Institution\Institution;
use App\Models\Institution\InstitutionType;
use App\Models\Tag;
use App\Services\HistoryService;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
    use WithPagination;

    public $search = '';
    public $sortColumn = 'display_name';
    public $sortDirection = 'asc';
    public $perPage = 10;

    #[Url(history: true)]
    public $tag_id;

    #[Url(history: true)]
    public $type_id;

    public function mount(): void {
        $this->authorize('viewAny', Institution::class);
    }

    public function boot(HistoryService $history): void {
        $this->history = $history;
    }

    public function clearFilters() {
        $this->reset('tag_id', 'type_id');
    }

    public function placeholder() {
        return <<<'HTML'
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <div class="placeholder placeholder-wave w-100"></div>
                        <div class="placeholder placeholder-wave w-100"></div>
                        <div class="placeholder placeholder-wave w-100"></div>
                        <div class="placeholder placeholder-wave w-100"></div>
                        <div class="placeholder placeholder-wave w-75"></div>
                        <div class="placeholder placeholder-wave w-50"></div>
                    </div>
                </div>
            </div>
        </div>
        HTML;
    }

    public function deleteInstitution($id) {
        $inst = Institution::findOrFail($id);

        if ($inst->hasAttachedClients()) {
            $this->dispatch('toast', message: 'Cannot delete: this institution is attached to one or more clients.', type: 'danger');
            return;
        }

        $inst->delete();

        $this->history->add($inst, "Institution deleted.");

        $this->history->add(auth()->user(), "Deleted institution {$inst->display_name}.");
    }

    public function render() {
        $institutions = Institution::with('type', 'dsarType')->withCount(['careSettings', 'claims']);

        if($this->search) {

            $search = trim($this->search);

            $institutions->where('display_name', 'like', '%'.$search.'%');

            if(is_numeric($search)) {
                $institutions->orWhere('id', $search - 100);
            }
        }

        if ($this->tag_id) {
            $institutions->whereHas('tags', function ($tags) {
                $tags->where('tags.id', $this->tag_id);
            });
        }

        if($this->type_id) {
            $institutions->where('institution_type_id', $this->type_id);
        }



        $institutions = $institutions->orderBy($this->sortColumn, $this->sortDirection)->paginate($this->perPage);
        return view('components.institution.index', [
            'institutions' => $institutions,
            'tags' => Tag::pluck('name', 'id')->toArray(),
            'types' => InstitutionType::pluck('display_name', 'id')->toArray(),
        ]);
    }
}
