<?php

namespace App\Modules\DefinedService\Route\Application\Index;

use App\Modules\DefinedService\Location\Domain\Location;
use App\Modules\DefinedService\Route\Application\RouteResponse;
use App\Modules\DefinedService\Route\Domain\IdRoute;
use App\Modules\DefinedService\Route\Domain\Route;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;

final class IndexRouteQueryHandler implements QueryHandler
{
    public function __construct(private RouteIndexer $indexer) {}

    public function __invoke(IndexRouteQuery $query): RouteResponse
    {
        $model = $this->indexer->__invoke(
            new IdRoute($query->id()),
            new IndexRoutePopulate($query->populate())
        );
        return new RouteResponse(
            $model->id()->value(),
            $model->date()->value(),
            $model->dateUpdate()->value(),
            $model->description()->value(),
            $model->version()->value(),
            $model->status()->value(),
            $model->idLocationStart()->value(),
            $model->idLocationEnd()->value(),
            $model->idTypeDefinedService()->value(),
            $this->locationStart($model),
            $this->locationEnd($model),
            $this->typeDefinedService($model)
        );
    }

    private function locationStart(Route $model): ?object
    {
        return $model->locationStart() ? $this->locationObject($model->locationStart()) : null;
    }

    private function locationEnd(Route $model): ?object
    {
        return $model->locationEnd() ? $this->locationObject($model->locationEnd()) : null;
    }

    private function locationObject(Location $model): ?object
    {
        return $model ? (object)[
            'Id_Location' => $model->id()->value(),
            'Location_Date' => $model->date()->value(),
            'Location_DateUpdate' => $model->dateUpdate()->value(),
            'Location_Name' => $model->name()->value(),
            'Location_Latitude' => $model->latitude()->value(),
            'Location_Longitude' => $model->longitude()->value(),
            'Location_Version' => $model->version()->value(),
            'Location_Status' => $model->status()->value(),
        ] : null;
    }

    private function typeDefinedService(Route $model): ?object
    {
        return $model->typeDefinedService() ? (object)[
            'Id_TypeDefinedService' => $model->typeDefinedService()->id()->value(),
            'TypeDefinedService_Date' => $model->typeDefinedService()->date()->value(),
            'TypeDefinedService_DateUpdate' => $model->typeDefinedService()->dateUpdate()->value(),
            'TypeDefinedService_Name' => $model->typeDefinedService()->name()->value(),
            'TypeDefinedService_Code' => $model->typeDefinedService()->code()->value(),
            'TypeDefinedService_Version' => $model->typeDefinedService()->version()->value(),
            'TypeDefinedService_Status' => $model->typeDefinedService()->status()->value(),
            'Id_TypeProvider' => $model->typeDefinedService()->idTypeProvider()->value()
        ] : null;
    }
}
