<?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 AppDocumentUserHydrator 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['name']) || (! empty($this->class->fieldMappings['name']['nullable']) && array_key_exists('name', $data))) {
            $value = $data['name'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['name']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['name']->setValue($document, $return);
            $hydratedData['name'] = $return;
        }

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

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

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

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

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

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

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

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

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

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

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

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

        // Field(type: "hash")
        if (isset($data['modulePermissions']) || (! empty($this->class->fieldMappings['modulePermissions']['nullable']) && array_key_exists('modulePermissions', $data))) {
            $value = $data['modulePermissions'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['modulePermissions']['type'];
                $return = (array) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['modulePermissions']->setValue($document, $return);
            $hydratedData['modulePermissions'] = $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;
    }
}