<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\Facility;
use DMD\LaLigaApi\Exception\ValidationException;
use phpDocumentor\Reflection\Types\Integer;

class FacilityDto
{
    public int $id;
    public string $name;
    public string $address;
    public array $availableHourList;
    public bool $active;
    public array $gameDtoList;
    public array $seasonDtoList;
    public \DateTimeImmutable $createdAt;
    public array $validationErrors;

    public function toArray(): array
    {
        return [
            'id' => $this->id ?? '',
            'name' => $this->name ?? '',
            'address' => $this->address ?? '',
            'availableHours' => $this->availableHourList ?? [],
            'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : ''
        ];
    }

    public function fillFromArray(array $dataList): void
    {
        if (isset($dataList['id']))
        {
            $this->id = $dataList['id'];
        }
        if (!empty($dataList['name']))
        {
            $this->name = $dataList['name'];
        }
        if (!empty($dataList['address']))
        {
            $this->address = $dataList['address'];
        }
        if (isset($dataList['active']))
        {
            $this->active = $dataList['active'];
        }
        if (isset($dataList['availableHours']))
        {
            $this->availableHourList = $dataList['availableHours'];
        }
        if (!empty($dataList['seasonList']))
        {
            foreach ($dataList['seasonList'] as $seasonItem)
            {
                $seasonDto = new SeasonDto();
                $seasonDto->fillFromArray($seasonItem);
                $this->seasonDtoList[] = $seasonDto;
            }
        }
        if (!empty($dataList['createdAt']))
        {
            $this->createdAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dataList['createdAt'], new \DateTimeZone('Europe/Madrid'));
        }
    }

    public function fillFromObject(Facility $facilityEntity): void
    {
        if ($facilityEntity->getId() !== null)
        {
            $this->id = $facilityEntity->getId();
        }
        if ($facilityEntity->getName() !== null)
        {
            $this->name = $facilityEntity->getName();
        }
        if ($facilityEntity->getAddress() !== null)
        {
            $this->address = $facilityEntity->getAddress();
        }
        if ($facilityEntity->getAvailableHours() !== null)
        {
            $this->availableHourList = $facilityEntity->getAvailableHours();
        }
        if ($facilityEntity->getCreatedAt() !== null)
        {
            $this->createdAt =$facilityEntity->getCreatedAt();
        }
    }

    public function validate(): void
    {
        if (empty($this->name))
        {
            $this->validationErrors[] = 'El nombre del pabellón no puede estar vacío.';
        }
        if (empty($this->address))
        {
            $this->validationErrors[] = 'La dirección del pabellón no puede estar vacío.';
        }
        if (empty($this->availableHourList))
        {
            $this->validationErrors[] = 'Las horas disponibles del pabellón no pueden estar vacías.';
        }
        if (!empty($this->validationErrors))
        {
            throw new ValidationException($this->validationErrors);
        }
    }
}