<?php

namespace App\Components;

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

class InstitutionRecordTypeManager extends Component {
    use AuthorizesRequests;

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

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

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

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

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

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

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

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

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

        $this->show = true;
    }

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

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

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

    public function deleteRecordType(HistoryService $service, int $id) {
        $this->authorize('institution-record-types.delete');

        $type = InstitutionRecordType::findOrFail($id);
        $name = $type->display_name;
        $type->delete();

        $service->add(auth()->user(), "Deleted Institution Record 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-record-type-manager');
    }
}
