<?php

namespace App\Service;

use App\DTO\TaskRequest;
use App\Entity\Task;
use App\Enum\TaskStatus;
use Doctrine\ORM\EntityManagerInterface;

class TaskManager
{
    public function __construct(private EntityManagerInterface $em)
    {
    }

    public function update(Task $task, TaskRequest $req): Task
    {
        $task->setName($req->name);
        $task->setDate($req->date);
        $task->setStatus($req->status);

        $this->em->flush();

        return $task;
    }

    public function delete(Task $task): void
    {
        $this->em->remove($task);
        $this->em->flush();
    }

    public function toggle(Task $task): Task
    {
        $new = match ($task->getRawStatus()) {
            TaskStatus::Active => TaskStatus::Done,
            TaskStatus::Done => TaskStatus::Active,
            TaskStatus::Inactive => TaskStatus::Inactive,
            TaskStatus::Past => TaskStatus::Past,
        };

        $task->setStatus($new);
        $this->em->flush();

        return $task;
    }

}
