<?php

namespace App\Livewire\Issues;

use App\Models\IssueChecklistTemplate;
use App\Models\Project;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.app')]
class IssueChecklistManager extends Component
{
    public Project $project;

    public $templates = [];

    public bool $showForm = false;

    public $editingId = null;

    public string $name = '';

    public array $items = [''];

    public function mount(Project $project)
    {
        $this->project = $project;
        abort_unless($this->canAccessProject() && Auth::user()->can('edit issues'), 403);
        $this->loadTemplates();
    }

    private function canAccessProject(): bool
    {
        $user = Auth::user();

        return $user->can('manage all')
            || $this->project->users()->where('user_id', $user->id)->exists();
    }

    public function loadTemplates(): void
    {
        $this->templates = IssueChecklistTemplate::where('project_id', $this->project->id)
            ->orderBy('name')->get();
    }

    public function newTemplate(): void
    {
        $this->reset(['editingId', 'name']);
        $this->items = [''];
        $this->resetErrorBag();
        $this->showForm = true;
    }

    public function edit($id): void
    {
        $t = IssueChecklistTemplate::where('project_id', $this->project->id)->findOrFail($id);
        $this->editingId = $t->id;
        $this->name = $t->name;
        $this->items = array_values($t->items ?: ['']) ?: [''];
        $this->resetErrorBag();
        $this->showForm = true;
    }

    public function addItemLine(): void
    {
        $this->items[] = '';
    }

    public function removeItemLine(int $i): void
    {
        unset($this->items[$i]);
        $this->items = array_values($this->items);
        if (empty($this->items)) {
            $this->items = [''];
        }
    }

    public function save(): void
    {
        $this->validate([
            'name' => 'required|string|max:255',
            'items' => 'required|array',
            'items.*' => 'nullable|string|max:255',
        ]);

        $items = array_values(array_filter(array_map('trim', $this->items), fn ($v) => $v !== ''));
        if (empty($items)) {
            $this->addError('items', 'Añade al menos una tarea.');

            return;
        }

        IssueChecklistTemplate::updateOrCreate(
            ['id' => $this->editingId, 'project_id' => $this->project->id],
            ['name' => $this->name, 'items' => $items, 'project_id' => $this->project->id],
        );

        $this->showForm = false;
        $this->loadTemplates();
        $this->dispatch('notify', 'Plantilla guardada');
    }

    public function delete($id): void
    {
        IssueChecklistTemplate::where('project_id', $this->project->id)->findOrFail($id)->delete();
        $this->loadTemplates();
        $this->dispatch('notify', 'Plantilla eliminada');
    }

    public function render()
    {
        return view('livewire.issues.issue-checklist-manager');
    }
}
