<?php

namespace App\Components\Ha;

use App\Models\Task;
use App\Services\HistoryService;
use App\Services\TaskService;
use Livewire\Attributes\Lazy;
use Livewire\Component;

/**
 * Dashboard column: an inline task list for the currently logged-in handler.
 * Tasks are keyed by Task.user_id (the assignee). This is the inline
 * counterpart to the header TaskManager widget.
 */
#[Lazy]
class HandlerTasks extends Component
{
    public $tasks;

    public bool $showAdd = false;

    public string $title = '';
    public string $description = '';
    public string $due_at = '';

    public function mount(): void
    {
        $this->load();
    }

    public function showAddTask(): void
    {
        $this->showAdd = true;
    }

    public function hideAddTask(): void
    {
        $this->showAdd = false;
        $this->reset('title', 'description', 'due_at');
    }

    public function addTask(HistoryService $history, TaskService $service): void
    {
        $validated = $this->validate([
            'title'       => 'required|string|max:255',
            'description' => 'nullable|string|max:255',
            'due_at'      => 'nullable|date|after:now|before:+1 month',
        ]);

        $service->addTask(
            $history,
            auth()->user(),
            $validated['title'],
            $validated['due_at'] ?? null,
            $validated['description'] ?? null
        );

        $this->hideAddTask();
        $this->load();
    }

    public function completeTask($taskId, HistoryService $history, TaskService $service): void
    {
        $task = Task::withoutGlobalScope('outstanding')
            ->where('user_id', auth()->id())
            ->findOrFail($taskId);

        $service->completeTask($history, $task, $task->owner ?? auth()->user());

        $this->load();
    }

    public function deleteTask($taskId, HistoryService $history, TaskService $service): void
    {
        $task = Task::withoutGlobalScope('outstanding')
            ->where('user_id', auth()->id())
            ->find($taskId);

        if ($task) {
            $service->deleteTask($history, $task, $task->owner ?? auth()->user());
            $this->load();
        }
    }

    public function load(): void
    {
        $active = Task::withoutGlobalScope('outstanding')
            ->where('user_id', auth()->id())
            ->whereNull('completed_at')
            ->orderBy('due_at')
            ->get();

        $completed = Task::withoutGlobalScope('outstanding')
            ->where('user_id', auth()->id())
            ->whereNotNull('completed_at')
            ->latest('completed_at')
            ->limit(5)
            ->get();

        $this->tasks = $active->concat($completed);
    }

    public function render()
    {
        return view('components.ha.handler-tasks');
    }

    public function placeholder()
    {
        return <<<'HTML'
        <div class="placeholder-glow">
            <span class="placeholder col-12 d-block mb-1"></span>
            <span class="placeholder col-9 d-block mb-1"></span>
            <span class="placeholder col-6 d-block"></span>
        </div>
        HTML;
    }
}
