<?php

namespace Hydrators;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Hydrator\HydratorException;
use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
use Doctrine\ODM\MongoDB\Query\Query;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

use function array_key_exists;
use function gettype;
use function is_array;

/**
 * THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE.
 */
class AppDocumentEverestMissionHydrator implements HydratorInterface
{
    public function __construct(private DocumentManager $dm, private ClassMetadata $class) {}

    public function hydrate(object $document, array $data, array $hints = []): array
    {
        $hydratedData = [];

        // Field(type: "string")
        if (isset($data['reference']) || (! empty($this->class->fieldMappings['reference']['nullable']) && array_key_exists('reference', $data))) {
            $value = $data['reference'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['reference']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['reference']->setValue($document, $return);
            $hydratedData['reference'] = $return;
        }

        // Field(type: "int")
        if (isset($data['routeId']) || (! empty($this->class->fieldMappings['routeId']['nullable']) && array_key_exists('routeId', $data))) {
            $value = $data['routeId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['routeId']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['routeId']->setValue($document, $return);
            $hydratedData['routeId'] = $return;
        }

        // Field(type: "int")
        if (isset($data['routeIndex']) || (! empty($this->class->fieldMappings['routeIndex']['nullable']) && array_key_exists('routeIndex', $data))) {
            $value = $data['routeIndex'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['routeIndex']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['routeIndex']->setValue($document, $return);
            $hydratedData['routeIndex'] = $return;
        }

        // Field(type: "int")
        if (isset($data['countMission']) || (! empty($this->class->fieldMappings['countMission']['nullable']) && array_key_exists('countMission', $data))) {
            $value = $data['countMission'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['countMission']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['countMission']->setValue($document, $return);
            $hydratedData['countMission'] = $return;
        }

        // Field(type: "string")
        if (isset($data['status']) || (! empty($this->class->fieldMappings['status']['nullable']) && array_key_exists('status', $data))) {
            $value = $data['status'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['status']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['status']->setValue($document, $return);
            $hydratedData['status'] = $return;
        }

        // Field(type: "string")
        if (isset($data['internalStatus']) || (! empty($this->class->fieldMappings['internalStatus']['nullable']) && array_key_exists('internalStatus', $data))) {
            $value = $data['internalStatus'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['internalStatus']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['internalStatus']->setValue($document, $return);
            $hydratedData['internalStatus'] = $return;
        }

        // Field(type: "int")
        if (isset($data['agentId']) || (! empty($this->class->fieldMappings['agentId']['nullable']) && array_key_exists('agentId', $data))) {
            $value = $data['agentId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['agentId']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['agentId']->setValue($document, $return);
            $hydratedData['agentId'] = $return;
        }

        // Field(type: "string")
        if (isset($data['takeBackRef']) || (! empty($this->class->fieldMappings['takeBackRef']['nullable']) && array_key_exists('takeBackRef', $data))) {
            $value = $data['takeBackRef'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['takeBackRef']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['takeBackRef']->setValue($document, $return);
            $hydratedData['takeBackRef'] = $return;
        }

        // Field(type: "string")
        if (isset($data['initialMission']) || (! empty($this->class->fieldMappings['initialMission']['nullable']) && array_key_exists('initialMission', $data))) {
            $value = $data['initialMission'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['initialMission']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['initialMission']->setValue($document, $return);
            $hydratedData['initialMission'] = $return;
        }

        // Field(type: "date_immutable")
        if (isset($data['loadingDate']) || (! empty($this->class->fieldMappings['loadingDate']['nullable']) && array_key_exists('loadingDate', $data))) {
            $value = $data['loadingDate'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['loadingDate']['type'];
                if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateImmutableType::getDateTime($value); }
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['loadingDate']->setValue($document, $return);
            $hydratedData['loadingDate'] = $return;
        }

        // Field(type: "int")
        if (isset($data['loadingCode']) || (! empty($this->class->fieldMappings['loadingCode']['nullable']) && array_key_exists('loadingCode', $data))) {
            $value = $data['loadingCode'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['loadingCode']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['loadingCode']->setValue($document, $return);
            $hydratedData['loadingCode'] = $return;
        }

        // ReferenceOne
        if (isset($data['agency']) || (! empty($this->class->fieldMappings['agency']['nullable']) && array_key_exists('agency', $data))) {
            $return = $data['agency'];
            if ($return !== null) {
                if ($this->class->fieldMappings['agency']['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID && ! is_array($return)) {
                    throw HydratorException::associationTypeMismatch('App\Document\EverestMission', 'agency', 'array', gettype($return));
                }

                $className = $this->dm->getClassNameForAssociation($this->class->fieldMappings['agency'], $return);
                $identifier = ClassMetadata::getReferenceId($return, $this->class->fieldMappings['agency']['storeAs']);
                $targetMetadata = $this->dm->getClassMetadata($className);
                $id = $targetMetadata->getPHPIdentifierValue($identifier);
                $return = $this->dm->getReference($className, $id);
            }

            $this->class->propertyAccessors['agency']->setValue($document, $return);
            $hydratedData['agency'] = $return;
        }

        $mapping = $this->class->fieldMappings['expedition'];
        $className = $mapping['targetDocument'];
        $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
        $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']];
        $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'], $mapping['mappedBy']);
        $criteria = array_merge(
            [$mappedByFieldName => $data['_id']],
            $this->class->fieldMappings['expedition']['criteria'] ?? []
        );
        $sort = $this->class->fieldMappings['expedition']['sort'] ?? [];
        $return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, [], 0, $sort);
        $this->class->propertyAccessors['expedition']->setValue($document, $return);
        $hydratedData['expedition'] = $return;

        $mapping = $this->class->fieldMappings['returnShipment'];
        $className = $mapping['targetDocument'];
        $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
        $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']];
        $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'], $mapping['mappedBy']);
        $criteria = array_merge(
            [$mappedByFieldName => $data['_id']],
            $this->class->fieldMappings['returnShipment']['criteria'] ?? []
        );
        $sort = $this->class->fieldMappings['returnShipment']['sort'] ?? [];
        $return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, [], 0, $sort);
        $this->class->propertyAccessors['returnShipment']->setValue($document, $return);
        $hydratedData['returnShipment'] = $return;

        // Field(type: "int")
        if (isset($data['clientId']) || (! empty($this->class->fieldMappings['clientId']['nullable']) && array_key_exists('clientId', $data))) {
            $value = $data['clientId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['clientId']['type'];
                $return = (int) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['clientId']->setValue($document, $return);
            $hydratedData['clientId'] = $return;
        }

        // Field(type: "string")
        if (isset($data['oldObjectId']) || (! empty($this->class->fieldMappings['oldObjectId']['nullable']) && array_key_exists('oldObjectId', $data))) {
            $value = $data['oldObjectId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['oldObjectId']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['oldObjectId']->setValue($document, $return);
            $hydratedData['oldObjectId'] = $return;
        }

        // Field(type: "id")
        if (isset($data['_id']) || (! empty($this->class->fieldMappings['id']['nullable']) && array_key_exists('_id', $data))) {
            $value = $data['_id'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['id']['type'];
                $return = $value instanceof \MongoDB\BSON\ObjectId ? (string) $value : $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['id']->setValue($document, $return);
            $hydratedData['id'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('createdAt', $data) && ($data['createdAt'] !== null || ($this->class->fieldMappings['createdAt']['nullable'] ?? false))) {
            $value = $data['createdAt'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['createdAt']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['createdAt'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('updatedAt', $data) && ($data['updatedAt'] !== null || ($this->class->fieldMappings['updatedAt']['nullable'] ?? false))) {
            $value = $data['updatedAt'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['updatedAt']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['updatedAt'] = $return;
        }

        return $hydratedData;
    }
}