<?php

namespace Signteer\Core\Action\Sheet;

use Signteer\Core\Action\ActionResult;
// use Signteer\Core\Criteria\LocationCriteria;
// use Signteer\Core\Event\EventBus;
// use Signteer\Core\Event\Location\LocationCreated;
// use Signteer\Core\Finder\LocationFinder;
use Signteer\Core\Writer\SheetWriter;

final class UpdateSheet
{
    // public $finder = LocationFinder::class;
    public $writer = SheetWriter::class;
    // public $eventBus = EventBus::class;

    public function __invoke(int $id, string $title, string $description): ActionResult
    {
        $ret = new ActionResult();

        $title = trim($title);
        $description = trim($description);

        if (!strlen($title)) {
            return $ret->addError('__Sheet title is required.__');
        }

        // $c = (new LocationCriteria())->whereTitle('=', $title);
        // $already = $this->finder->exists($c);
        // if ($already) {
            // return $ret->addError('__Location title is already in use.__');
        // }

        $this->writer->update($id, $title, $description);

        // $this->eventBus->emit(new LocationCreated($id, $title));

        return $ret;
    }
}
