<?php

namespace App\Components;

use App\Models\Institution\InstitutionType;
use App\Services\HistoryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class InstitutionTypeManager extends Component {
    use AuthorizesRequests;

    public $institutionTypes = [];
    public ?string $name = '';

    public bool $show = false;
    public bool $editing = false;
    public $institutionType;

    protected array $rules = [
        'name' => 'required|string|max:255',
    ];

    public function mount() {
        $this->authorize('institution-types.view');
        $this->loadData();
    }

    public function loadData() {
        $this->institutionTypes = InstitutionType::orderBy('display_name')->get()->toArray();
    }

    public function showAdd() {
        $this->resetErrorBag();
        $this->editing = false;
        $this->show = true;
    }

    public function hideAdd() {
        $this->reset('name', 'editing', 'institutionType');
        $this->show = false;
    }

    public function loadEdit(int $id) {
        $this->resetErrorBag();
        $type = InstitutionType::find($id);

        $this->institutionType = $type;
        $this->name = $type->display_name;
        $this->editing = true;

        $this->show = true;
    }

    public function save(HistoryService $historyService) {
        $this->authorize($this->editing ? 'institution-types.edit' : 'institution-types.create');
        $this->validate();

        if ($this->editing) {
            $this->institutionType->update([
                'display_name' => $this->name,
            ]);
            $historyService->add(auth()->user(), "Updated Institution Type '{$this->name}'");
        } else {
            InstitutionType::create([
                'display_name' => $this->name,
            ]);
            $historyService->add(auth()->user(), "Created Institution Type '{$this->name}'");
        }

        $this->loadData();
        $this->hideAdd();
    }

    public function deleteInstitutionType(HistoryService $service, int $id) {
        $this->authorize('institution-types.delete');
        $type = InstitutionType::findOrFail($id);
        $name = $type->display_name;
        $type->delete();

        $service->add(auth()->user(), "Deleted Institution Type '{$name}'");
        $this->loadData();
    }

    public function placeholder() {
        return <<<'HTML'
        <div class="row">
            <div class="col-12 col-md-6 offset-md-3">
                <div class="card">
                    <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-8"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        HTML;
    }

    public function render() {
        return view('components.institution-type-manager');
    }
}
