<?php

namespace App\Monitor\Action\Handler;

use App\Monitor\Action\Command\AddMonitorCommand;
use App\Monitor\Action\Command\DeleteMonitorCommand;
use App\Monitor\Action\Result\AddMonitorResult;
use App\Monitor\Action\Result\DeleteMonitorResult;
use App\Monitor\Framework\Entity\Monitor;
use App\Monitor\Framework\Repository\MonitorRepository;
use App\User\Framework\Repository\UserRepository;
use DateTimeImmutable;
use OneToMany\RichBundle\Contract\CommandInterface;
use OneToMany\RichBundle\Contract\HandlerInterface;
use OneToMany\RichBundle\Contract\ResultInterface;

/** @implements HandlerInterface<DeleteMonitorCommand, DeleteMonitorResult> */
readonly class DeleteMonitorHandler implements HandlerInterface
{
    public function __construct(
        private MonitorRepository $monitorRepository,
    ) {}

    public function handle(CommandInterface $command): ResultInterface
    {
        $monitor = $this->monitorRepository->find($command->monitorId);
        $this->monitorRepository->getEntityManager()->remove($monitor);
        $this->monitorRepository->getEntityManager()->flush();

        return new DeleteMonitorResult(
            status: 'OK',
            result: [],
            monitor: $monitor
        );
    }
}
