<?php

namespace App\Tests\Entity;

use App\Entity\Customer;
use App\Entity\Domain;
use App\Entity\DomainEmail;
use App\Entity\User;
use PHPUnit\Framework\TestCase;

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

        return new Domain(new Customer($user), 'e-cosplay.fr');
    }

    public function testConstructor(): void
    {
        $domain = $this->createDomain();
        $email = new DomainEmail($domain, ' Contact ');

        $this->assertNull($email->getId());
        $this->assertSame($domain, $email->getDomain());
        $this->assertSame('contact', $email->getName());
        $this->assertSame('contact@e-cosplay.fr', $email->getFullEmail());
        $this->assertSame(DomainEmail::STATE_ACTIVE, $email->getState());
        $this->assertTrue($email->isActive());
        $this->assertSame(5120, $email->getQuotaMb());
        $this->assertInstanceOf(\DateTimeImmutable::class, $email->getCreatedAt());
        $this->assertNull($email->getUpdatedAt());
    }

    public function testSetters(): void
    {
        $email = new DomainEmail($this->createDomain(), 'info');

        $email->setName(' Support ');
        $this->assertSame('support', $email->getName());
        $this->assertSame('support@e-cosplay.fr', $email->getFullEmail());

        $email->setQuotaMb(10240);
        $this->assertSame(10240, $email->getQuotaMb());

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

    public function testState(): void
    {
        $email = new DomainEmail($this->createDomain(), 'test');

        $this->assertTrue($email->isActive());

        $email->setState(DomainEmail::STATE_SUSPENDED);
        $this->assertSame(DomainEmail::STATE_SUSPENDED, $email->getState());
        $this->assertFalse($email->isActive());
        $this->assertInstanceOf(\DateTimeImmutable::class, $email->getUpdatedAt());

        $email->setState(DomainEmail::STATE_DISABLED);
        $this->assertSame(DomainEmail::STATE_DISABLED, $email->getState());
        $this->assertFalse($email->isActive());
    }
}
