<?php

namespace App\Components\Institution;

use App\Models\Institution\Institution;
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;


    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;
        }

        $context = [
            'institution_id' => $inst->id,
            'display_name'   => $inst->display_name,
        ];

        $inst->delete();

        // The institution's own history cascades away with it, so record the
        // deletion against the acting user only.
        $this->history->addForActor("Deleted institution \"{$context['display_name']}\"", $context);
    }

    public function render() {
        $institutions = Institution::with('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);
            });
        }

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