<?php

namespace Planaday\Database\SQLDatabaseControllers\Location;

use DateTime;
use DateTimeZone;
use Planaday\Entities\CostsData;
use Planaday\Entities\Location\DTO\Location;

class LocationDatabaseObject {
	/**
	 * @param Location $location
	 *
	 * @return array<mixed>
	 */
	public static function getDatabaseArrayFromLocation(Location $location): array {
		$column = LocationColumnEnum::class;

		return array(
			$column::COST => $location->getCostData()->getCost(),
			$column::VAT => $location->getCostData()->getVat(),
			$column::VAT_CODE => $location->getCostData()->getVatCode(),
			$column::ID => $location->getId(),
			$column::NAME => $location->getName(),
			$column::CODE => $location->getCode(),
			$column::STREET_1 => $location->getStreet1(),
			$column::STREET_2 => $location->getStreet2(),
			$column::HOUSE_NUMBER => $location->getHouseNumber(),
			$column::HOUSE_NUMBER_EXTENSION => $location->getHouseNumberExtension(),
			$column::ZIPCODE           => $location->getZipcode(),
			$column::CITY              => $location->getCity(),
			$column::COUNTRY           => $location->getCountry(),
			$column::LAT               => $location->getLat(),
			$column::LNG               => $location->getLng(),
			$column::PHONE_NUMBER      => $location->getPhoneNumber(),
			$column::EMAIL             => $location->getEmail(),
			$column::WEBSITE           => $location->getWebsite(),
			$column::DESCRIPTION       => $location->getDescription(),
			$column::CAPACITY          => $location->getCapacity(),
			$column::CAN_BE_OVERBOOKED => $location->isOverbookable(),
			$column::IS_VIRTUAL        => $location->isVirtual(),
			$column::IS_EXTERNAL       => $location->isExternal(),
			$column::LAST_UPDATE       => $location->getLastUpdate()->format('Y-m-d H:i:s')
		);
	}

	/**
	 * @param array<mixed> $data
	 *
	 * @return Location
	 */
	public static function getLocationObject(array $data): Location{
		$column = LocationColumnEnum::class;

		$lastUpdatedTime = DateTime::createFromFormat(
			"Y-m-d H:i:s",
			$data[LocationColumnEnum::LAST_UPDATE],
			new DateTimeZone('Europe/Amsterdam')
		);

		if ( $lastUpdatedTime === false )
			$lastUpdatedTime = (new DateTime())->setTimestamp(0);

		return (new Location($data[$column::ID]))
			->setName($data[$column::NAME])
			->setCode($data[$column::CODE])
			->setStreet1($data[$column::STREET_1])
			->setStreet2($data[$column::STREET_2])
			->setHouseNumber($data[$column::HOUSE_NUMBER])
			->setHouseNumberExtension($data[$column::HOUSE_NUMBER_EXTENSION])
			->setZipcode($data[$column::ZIPCODE])
			->setCity($data[$column::CITY])
			->setCountry($data[$column::COUNTRY])
			->setLat($data[$column::LAT])
			->setLng($data[$column::LNG])
			->setPhoneNumber($data[$column::PHONE_NUMBER])
			->setEmail($data[$column::EMAIL])
			->setWebsite($data[$column::WEBSITE])
			->setDescription($data[$column::DESCRIPTION])
			->setCapacity($data[$column::CAPACITY])
			->setOverbookable($data[$column::CAN_BE_OVERBOOKED])
			->setIsVirtual($data[$column::IS_VIRTUAL])
			->setIsExternal($data[$column::IS_EXTERNAL])
			->setCostData(
				(new CostsData())
					->setCost($data[$column::COST])
					->setVat($data[$column::VAT])
					->setVatCode($data[$column::VAT_CODE])
			)
			->setLastUpdate($lastUpdatedTime);
	}
}