<?php

namespace App\Tests\Entity;

use App\Entity\Customer;
use App\Entity\User;
use App\Entity\Website;
use PHPUnit\Framework\TestCase;

class WebsiteTest extends TestCase
{
    private function createCustomer(): Customer
    {
        $user = new User();
        $user->setEmail('c@t.com');
        $user->setFirstName('C');
        $user->setLastName('T');
        $user->setPassword('h');

        return new Customer($user);
    }

    public function testConstructor(): void
    {
        $customer = $this->createCustomer();
        $site = new Website($customer, 'Mon Site');

        $this->assertNull($site->getId());
        $this->assertSame($customer, $site->getCustomer());
        $this->assertSame('Mon Site', $site->getName());
        $this->assertSame(36, \strlen($site->getUuid()));
        $this->assertSame(Website::TYPE_VITRINE, $site->getType());
        $this->assertSame(Website::STATE_CREATED, $site->getState());
        $this->assertInstanceOf(\DateTimeImmutable::class, $site->getCreatedAt());
        $this->assertNull($site->getUpdatedAt());
    }

    public function testConstructorEcommerce(): void
    {
        $site = new Website($this->createCustomer(), 'Boutique', Website::TYPE_ECOMMERCE);
        $this->assertSame(Website::TYPE_ECOMMERCE, $site->getType());
    }

    public function testSetters(): void
    {
        $site = new Website($this->createCustomer(), 'Test');

        $site->setName('Nouveau nom');
        $this->assertSame('Nouveau nom', $site->getName());

        $site->setType(Website::TYPE_ECOMMERCE);
        $this->assertSame(Website::TYPE_ECOMMERCE, $site->getType());

        $now = new \DateTimeImmutable();
        $site->setUpdatedAt($now);
        $this->assertSame($now, $site->getUpdatedAt());
    }

    public function testState(): void
    {
        $site = new Website($this->createCustomer(), 'Test');
        $this->assertFalse($site->isOpen());

        $site->setState(Website::STATE_INSTALL_PROGRESS);
        $this->assertSame(Website::STATE_INSTALL_PROGRESS, $site->getState());
        $this->assertFalse($site->isOpen());

        $site->setState(Website::STATE_OPEN);
        $this->assertTrue($site->isOpen());
        $this->assertInstanceOf(\DateTimeImmutable::class, $site->getUpdatedAt());

        $site->setState(Website::STATE_SUSPENDED);
        $this->assertFalse($site->isOpen());

        $site->setState(Website::STATE_CLOSED);
        $this->assertFalse($site->isOpen());
    }

    public function testUuidUnique(): void
    {
        $customer = $this->createCustomer();
        $site1 = new Website($customer, 'Site 1');
        $site2 = new Website($customer, 'Site 2');

        $this->assertNotSame($site1->getUuid(), $site2->getUuid());
    }

    public function testRevendeurCode(): void
    {
        $site = new Website($this->createCustomer(), 'Test');

        $this->assertNull($site->getRevendeurCode());

        $result = $site->setRevendeurCode('REV01');
        $this->assertSame('REV01', $site->getRevendeurCode());
        $this->assertSame($site, $result);

        $site->setRevendeurCode(null);
        $this->assertNull($site->getRevendeurCode());
    }
}
