<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Dto;

use Ferienpass\AdminBundle\Dto\OrganizationDataDto;
use Ferienpass\CoreBundle\Entity\Host;
use Ferienpass\CoreBundle\Entity\PostalAddress;
use PHPUnit\Framework\TestCase;

final class OrganizationDataDtoTest extends TestCase
{
    private function makeHostWithAddress(): Host
    {
        $host = new Host();
        $host->setName('Test Organization');
        $host->setPhone('+4930123456');
        $host->setEmail('test@example.com');
        $host->setWebsite('https://test.example.com');
        $host->setText('Some text');

        $address = new PostalAddress();
        $address->setStreet('Main Street 1');
        $address->setPostalCode('12345');
        $address->setCity('Berlin');
        $address->setCountry('DE');
        $host->addPostalAddress($address);

        return $host;
    }

    public function testFromEntityPopulatesDtoFromHost(): void
    {
        $host = $this->makeHostWithAddress();

        $dto = new OrganizationDataDto();
        $dto->fromEntity($host);

        self::assertSame('Test Organization', $dto->name);
        self::assertSame('+4930123456', $dto->phone);
        self::assertSame('test@example.com', $dto->email);
        self::assertSame('https://test.example.com', $dto->website);
        self::assertSame('Some text', $dto->text);
        self::assertSame('Main Street 1', $dto->street);
        self::assertSame('12345', $dto->postalCode);
        self::assertSame('Berlin', $dto->city);
        self::assertSame('DE', $dto->country);
    }

    public function testFromEntityHandlesHostWithNoPostalAddress(): void
    {
        $host = new Host();
        $host->setName('No Address Org');

        $dto = new OrganizationDataDto();
        $dto->fromEntity($host);

        self::assertSame('No Address Org', $dto->name);
        self::assertNull($dto->street);
    }

    public function testToEntityAppliesDtoPropertiesToHost(): void
    {
        $host = new Host();
        $host->getPostalAddresses()->add(new PostalAddress());

        $dto = new OrganizationDataDto();
        $dto->name = 'Updated Org';
        $dto->email = 'updated@example.com';
        $dto->street = 'New Street 5';
        $dto->postalCode = '54321';
        $dto->city = 'Hamburg';
        $dto->country = 'DE';

        $dto->toEntity($host);

        self::assertSame('Updated Org', $host->getName());
        self::assertSame('updated@example.com', $host->getEmail());
        self::assertSame('New Street 5', $host->getPostalAddresses()->first()->getStreet());
        self::assertSame('54321', $host->getPostalAddresses()->first()->getPostalCode());
        self::assertSame('Hamburg', $host->getPostalAddresses()->first()->getCity());
    }

    public function testToEntityCreatesPostalAddressIfNoneExists(): void
    {
        $host = new Host(); // no postal addresses

        $dto = new OrganizationDataDto();
        $dto->name = 'New Org';
        $dto->city = 'Munich';

        $dto->toEntity($host);

        self::assertCount(1, $host->getPostalAddresses());
        self::assertSame('Munich', $host->getPostalAddresses()->first()->getCity());
    }
}
