<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\Facility;
use DMD\LaLigaApi\Entity\Team;
use DMD\LaLigaApi\Exception\ValidationException;

class TeamDto
{
    public int $id;
    public string $name;
    public bool $active;
    public array $seasonDtoList;
    public array $playerDtoList;
    public array $validationErrors;
    public UserDto $captainDto;
    public \DateTimeImmutable $createdAt;

    public function toArray(): array
    {
        $seasonList = [];
        $playerList = [];
        if (!empty($this->seasonDtoList))
        {
            foreach ($this->seasonDtoList as $seasonDto)
            {
                $seasonList = $seasonDto->toArray();
            }
        }
        if (!empty($this->playerDtoList))
        {
            foreach ($this->playerDtoList as $playerDto)
            {
                $playerList = $playerDto->toArray();
            }
        }
        return [
            'name' => $this->name ?? '',
            'playerList' => $playerList,
            'seasonList' => $seasonList,
            'captainId' => $this->captainDto?->id,
            'createdAt' => $this->createdAt->format('Y-m-d')
        ];
    }

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

    public function fillFromObject(Team $teamEntity): void
    {
        if ($teamEntity->getId())
        {
            $this->id = $teamEntity->getId();
        }
        if ($teamEntity->getName())
        {
            $this->name = $teamEntity->getName();
        }
        if ($teamEntity->getPlayers())
        {
            foreach ($teamEntity->getPlayers() as $playerEntity)
            {
                $playerDto = new PlayerDto();
                $playerDto->fillFromObject($playerEntity);
                $this->playerDtoList[] = $playerDto;
            }
        }
        if ($teamEntity->getCreatedAt())
        {
            $this->createdAt = $teamEntity->getCreatedAt();
        }
    }

    public function validate(): void
    {
        if (empty($this->name))
        {
            $this->validationErrors[] = 'El nombre del equipo no puede estar vacío.';
        }
        if (!empty($this->validationErrors))
        {
            throw new ValidationException($this->validationErrors);
        }
    }
}