<?php

namespace App\Tests\Entity;

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

class DomainTest 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();
        $domain = new Domain($customer, 'Example.COM ');

        $this->assertNull($domain->getId());
        $this->assertSame($customer, $domain->getCustomer());
        $this->assertSame('example.com', $domain->getFqdn());
        $this->assertNull($domain->getRegistrar());
        $this->assertNull($domain->getZoneCloudflare());
        $this->assertNull($domain->getZoneIdCloudflare());
        $this->assertNull($domain->getExpiredAt());
        $this->assertFalse($domain->isGestion());
        $this->assertFalse($domain->isBilling());
        $this->assertInstanceOf(\DateTimeImmutable::class, $domain->getCreatedAt());
        $this->assertNull($domain->getUpdatedAt());
    }

    public function testSetters(): void
    {
        $domain = new Domain($this->createCustomer(), 'e-cosplay.fr');

        $domain->setFqdn(' ESY-WEB.DEV ');
        $this->assertSame('esy-web.dev', $domain->getFqdn());

        $domain->setRegistrar('OVH');
        $this->assertSame('OVH', $domain->getRegistrar());

        $domain->setZoneCloudflare('active');
        $this->assertSame('active', $domain->getZoneCloudflare());

        $domain->setZoneIdCloudflare('zone_abc123');
        $this->assertSame('zone_abc123', $domain->getZoneIdCloudflare());

        $expiry = new \DateTimeImmutable('2027-01-01');
        $domain->setExpiredAt($expiry);
        $this->assertSame($expiry, $domain->getExpiredAt());

        $domain->setIsGestion(true);
        $this->assertTrue($domain->isGestion());

        $domain->setIsBilling(true);
        $this->assertTrue($domain->isBilling());

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

    public function testIsExpired(): void
    {
        $domain = new Domain($this->createCustomer(), 'test.fr');

        $this->assertFalse($domain->isExpired());

        $domain->setExpiredAt(new \DateTimeImmutable('-1 day'));
        $this->assertTrue($domain->isExpired());

        $domain->setExpiredAt(new \DateTimeImmutable('+1 year'));
        $this->assertFalse($domain->isExpired());
    }

    public function testIsExpiringSoon(): void
    {
        $domain = new Domain($this->createCustomer(), 'test.fr');

        $this->assertFalse($domain->isExpiringSoon());

        $domain->setExpiredAt(new \DateTimeImmutable('+15 days'));
        $this->assertTrue($domain->isExpiringSoon(30));

        $domain->setExpiredAt(new \DateTimeImmutable('+60 days'));
        $this->assertFalse($domain->isExpiringSoon(30));
    }
}
