<?php

namespace App\Components;

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

#[Lazy]
class TaskManager extends Component {

    public $tasks;
    public $task;
    public bool $open = false;
    public bool $showAdd = false;
    public int $outstanding = 0;
    public int $overdue = 0;
    public int $soon = 0;
    public int $good = 0;

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

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

    public function showTaskManager() {
        $this->open = true;
        $this->showAdd = false;
    }

    public function hideTaskManager() {
        $this->open = false;
        $this->showAdd = false;
    }

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

    public function hideAddTask() {
        $this->showAdd = false;
    }

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

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

        $this->hideAddTask();

        $this->reset('title', 'description', 'due_at');

        if ($task->due_at->isPast()) {
            $this->overdue++;
        } elseif ($task->due_at->isBefore(now()->addDays(2))) {
            $this->soon++;
        } else {
            $this->good++;
        }

        $this->tasks->push($task);
    }

    public function completeTask($taskId) {
        $task = Task::find($taskId);

        if ($task) {
            $task->update(['completed_at' => now()]);
            $this->load();
        }
    }

    public function deleteTask($taskId) {
        $task = Task::find($taskId);

        if ($task) {
            $task->delete();
            $this->load();
        }
    }

    public function load() {
        $tasks = Task::where('user_id', auth()->id())->get();
        $this->tasks = $tasks;
        $this->overdue = Task::where('user_id', auth()->id())->where('due_at', '<', now())->count();
        $this->soon = Task::where('user_id', auth()->id())->whereBetween('due_at', [now(), now()->addDays(2)])->count();
        $this->good = Task::where('user_id', auth()->id())->where('due_at', '>', now()->addDays(2))->count();
        $this->outstanding = $this->overdue + $this->good + $this->soon;
    }

    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;

    }

    public function render() {
        return view('components.task-manager');
    }
}
