<?php

namespace App\Components\Tag;

use App\Interface\HistoryOwner;
use App\Models\Tag;
use App\Services\HistoryService;
use Illuminate\Database\Eloquent\Model;
use Livewire\Component;

class TagAssigner extends Component {
    public Model $model;
    public $search = '';

    public function mount(Model $model) {
        $this->model = $model;
        $this->model->load('tags');
    }

    public function getAvailableTagsProperty()
    {
        return Tag::whereNotIn('id', $this->model->tags->pluck('id'))
            ->when($this->search, fn($q) => $q->where('name', 'like', '%' . $this->search . '%'))
            ->orderBy('name')
            ->get();
    }

    public function assignTag(HistoryService $history, $tagId) {
        $tag = Tag::findOrFail($tagId);

        $this->model->tags()->syncWithoutDetaching([$tagId]);
        $this->model->load('tags');
        $this->search = '';

        if ($this->model instanceof HistoryOwner) {
            $history->add($this->model, "Tag added: {$tag->name}", [
                'tag_id'   => $tag->id,
                'tag_name' => $tag->name,
            ]);
        }
    }

    public function removeTag(HistoryService $history, $tagId) {
        $tag = Tag::findOrFail($tagId);

        $this->model->tags()->detach($tagId);
        $this->model->load('tags');

        if ($this->model instanceof HistoryOwner) {
            $history->add($this->model, "Tag removed: {$tag->name}", [
                'tag_id'   => $tag->id,
                'tag_name' => $tag->name,
            ]);
        }
    }

    public function render() {
        return view('components.tag.tag-assigner', [
            'availableTags' => $this->availableTags,
        ]);
    }
}