<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\User;

class UserDto
{
    public int $id;
    public array $roles;
    public string $email;
    public string $password;
    public string $firstName;
    public string $lastName;
    public string $phone;
    public string $profilePicture;
    public \DateTimeInterface $birthday;
    public array $noteList;
    public bool $active;
    public array $receivedNotificationDtoList;
    public array $validationErrors;

    public function toArray(): array
    {
        if (!empty($this->receivedNotificationDtoList))
        {
            foreach ($this->receivedNotificationDtoList as $notificationDto)
            {
                $notificationArray[] = $notificationDto->toArray();
            }
        }
        return [
            'id' => $this->id ?? null,
            'email' => $this->email ?? null,
            'firstName' => $this->firstName ?? null,
            'lastName' => $this->lastName ?? null,
            'phone' => $this->phone,
            'profilePicture' => $this->profilePicture ?? null,
            'birthday' => $this->birthday->format('Y-m-d'),
            'noteList' => $this->noteList ?? [],
            'notificationList' => $notificationArray ?? []
        ];
    }

    public function toLoginArray(): array
    {
        return [
            'id' => $this->id ?? null,
            'email' => $this->email ?? null,
            'firstName' => $this->firstName ?? null,
            'lastName' => $this->lastName ?? null,
            'phone' => $this->phone,
            'profilePicture' => $this->profilePicture ?? null,
            'birthday' => $this->birthday->format('Y-m-d'),
            'unreadNotifications' => count($this->receivedNotificationDtoList)
        ];
    }

    public function toRegisterArray(): array
    {
        return [
            'id' => $this->id ?? null,
            'email' => $this->email ?? null,
            'firstName' => $this->firstName ?? null,
            'lastName' => $this->lastName ?? null,
            'phone' => $this->phone,
            'profilePicture' => $this->profilePicture ?? null,
            'birthday' => $this->birthday->format('Y-m-d'),
        ];
    }

    public function fillFromObject(User $userObj): void
    {
        if ($userObj->getId() !== null)
        {
            $this->id = $userObj->getId();
        }
        if ($userObj->getEmail() !== null)
        {
            $this->email = $userObj->getEmail();
        }
        if ($userObj->getFirstName() !== null)
        {
            $this->firstName = $userObj->getFirstName();
        }
        if ($userObj->getLastName() !== null)
        {
            $this->lastName = $userObj->getLastName();
        }
        if ($userObj->getPhone() !== null)
        {
            $this->phone = $userObj->getPhone();
        }
        if ($userObj->getProfilePicture() !== null)
        {
            $this->profilePicture = $userObj->getProfilePicture();
        }
        if ($userObj->getBirthday() !== null)
        {
            $this->birthday = $userObj->getBirthday();
        }
        if ($userObj->getReceivedNotifications() !== null)
        {
            foreach ($userObj->getReceivedNotifications() as $receivedNotificationEntity)
            {
                $notificationDto = new NotificationDto();
                $notificationDto->fillFromObj($receivedNotificationEntity);
                $this->receivedNotificationDtoList[] = $notificationDto;
            }
        }
        if ($userObj->getNoteList() !== null)
        {
            $this->noteList = $userObj->getNoteList();
        }
    }

    public function fillFromArray(array $dataList): void
    {
        if (!empty($dataList['id']))
        {
            $this->id = $dataList['id'];
        }
        if (!empty($dataList['email']))
        {
            $this->email = trim($dataList['email']);
        }
        if (!empty($dataList['password']))
        {
            $this->password = trim($dataList['password']);
        }
        if (!empty($dataList['firstName']))
        {
            $this->firstName = $dataList['firstName'];
        }
        if (!empty($dataList['lastName']))
        {
            $this->lastName = $dataList['lastName'];
        }
        if (!empty($dataList['phone']))
        {
            $this->phone = $dataList['phone'];
        }
        if (!empty($dataList['profilePicture']))
        {
            $this->profilePicture = $dataList['profilePicture'];
        }
        if (!empty($dataList['birthday']))
        {
            $this->birthday = \DateTime::createFromFormat('Y-m-d', $dataList['birthday']);
        }
        if (!empty($dataList['notes']))
        {
            $this->noteList = $dataList['notes'];
        }
        if (isset($dataList['active']))
        {
            $this->active = $dataList['active'];
        }
    }

    public function validate(): void
    {
        if (empty($this->email))
        {
            $this->validationErrors[] = 'El correo no puede estar vacío.';
        }
        if (!empty($this->email) && !(filter_var($this->email, FILTER_VALIDATE_EMAIL)))
        {
            $this->validationErrors[] = 'El correo no está en un formato válido.';
        }
        if (empty($this->firstName))
        {
            $this->validationErrors[] = 'El nombre no puede estar vacío';
        }
        if (empty($this->lastName))
        {
            $this->validationErrors[] = 'El apellido no puede estar vacío';
        }
        if (strlen($this->phone) !== 9)
        {
            $this->validationErrors[] = 'El número de teléfono debe tener 9 dígitos.';
        }
        if (!preg_match('@[A-Z]@', $this->password))
        {
            $this->validationErrors[] = 'La contraseña debe contener al menos una letra mayúscula.';
        }
        if (!preg_match('@[0-9]@', $this->password))
        {
            $this->validationErrors[] = 'La contraseña debe contener al menos un número.';
        }
        if (!preg_match('@\W@', $this->password))
        {
            $this->validationErrors[] = 'La contraseña debe contener al menos un caracter especial.';
        }
        if (strlen($this->password) < 8)
        {
            $this->validationErrors[] = 'La contraseña debe contener al menos 8 caracteres.';
        }
    }
}