<?php

namespace App\Modules\DefinedService\OfficesBriefing\Application\Insert;

use App\Modules\DefinedService\OfficesBriefing\Domain\IdLocation;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeCapacity;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeColor;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeDate;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeDateUpdate;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeName;
use App\Modules\DefinedService\OfficesBriefing\Domain\BriefingOfficeStatus;
use App\Modules\DefinedService\OfficesBriefing\Domain\IdBriefingOffice;
use App\Modules\DefinedService\OfficesBriefing\Domain\ParentOfficeId;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;

final class InsertOfficesBriefingQueryHandler implements QueryHandler
{
    public function __construct(private OfficesBriefingInserter $inserter) {}

    public function __invoke(InsertOfficesBriefingQuery $query): InsertOfficesBriefingResponse
    {
        $color    = $query->color()    ?? '';
        $capacity = $query->capacity() ?? 0;
        $parentId = $query->parentOfficeId();
        if ($parentId === null) {
            $parentId = 0;
        }

        return new InsertOfficesBriefingResponse(
            $this->inserter->__invoke(
                new IdBriefingOffice(0),
                new BriefingOfficeDate($query->date()),
                new BriefingOfficeDateUpdate($query->dateUpdate()),
                new BriefingOfficeName($query->name()),
                new BriefingOfficeStatus($query->status()),
                new BriefingOfficeColor($color),
                new BriefingOfficeCapacity($capacity),
                new ParentOfficeId($parentId),
                new IdLocation($query->idLocation()),
                $query->idAdmin()
            )
        );
    }
}
