<?php

namespace App\Components;

use App\Models\Document\DocumentType;
use App\Services\HistoryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class DocumentTypeManager extends Component {
    use AuthorizesRequests;

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

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

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

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

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

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

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

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

        $this->documentType = $docType;
        $this->name = $docType->display_name;
        $this->editing = true;

        $this->show = true;
    }

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

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

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

    public function deleteDocumentType(HistoryService $service, int $id) {
        $this->authorize('doc-types.delete');
        $docType = DocumentType::findOrFail($id);
        $name = $docType->name;
        $docType->delete();

        $service->add(auth()->user(), "Deleted Document 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.document-type-manager');
    }
}