<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Domain
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Customer::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Customer $customer;

    #[ORM\Column(length: 255, unique: true)]
    private string $fqdn;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $registrar = null;

    #[ORM\Column(length: 50, nullable: true)]
    private ?string $zoneCloudflare = null;

    #[ORM\Column(length: 100, nullable: true)]
    private ?string $zoneIdCloudflare = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $expiredAt = null;

    #[ORM\Column]
    private bool $isGestion = false;

    #[ORM\Column]
    private bool $isBilling = false;

    #[ORM\Column]
    private \DateTimeImmutable $createdAt;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $updatedAt = null;

    public function __construct(Customer $customer, string $fqdn)
    {
        $this->customer = $customer;
        $this->fqdn = strtolower(trim($fqdn));
        $this->createdAt = new \DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCustomer(): Customer
    {
        return $this->customer;
    }

    public function getFqdn(): string
    {
        return $this->fqdn;
    }

    public function setFqdn(string $fqdn): static
    {
        $this->fqdn = strtolower(trim($fqdn));

        return $this;
    }

    public function getRegistrar(): ?string
    {
        return $this->registrar;
    }

    public function setRegistrar(?string $registrar): static
    {
        $this->registrar = $registrar;

        return $this;
    }

    public function getZoneCloudflare(): ?string
    {
        return $this->zoneCloudflare;
    }

    public function setZoneCloudflare(?string $zoneCloudflare): static
    {
        $this->zoneCloudflare = $zoneCloudflare;

        return $this;
    }

    public function getZoneIdCloudflare(): ?string
    {
        return $this->zoneIdCloudflare;
    }

    public function setZoneIdCloudflare(?string $zoneIdCloudflare): static
    {
        $this->zoneIdCloudflare = $zoneIdCloudflare;

        return $this;
    }

    public function getExpiredAt(): ?\DateTimeImmutable
    {
        return $this->expiredAt;
    }

    public function setExpiredAt(?\DateTimeImmutable $expiredAt): static
    {
        $this->expiredAt = $expiredAt;

        return $this;
    }

    public function isGestion(): bool
    {
        return $this->isGestion;
    }

    public function setIsGestion(bool $isGestion): static
    {
        $this->isGestion = $isGestion;

        return $this;
    }

    public function isBilling(): bool
    {
        return $this->isBilling;
    }

    public function setIsBilling(bool $isBilling): static
    {
        $this->isBilling = $isBilling;

        return $this;
    }

    public function getCreatedAt(): \DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): ?\DateTimeImmutable
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    public function isExpired(): bool
    {
        return null !== $this->expiredAt && $this->expiredAt < new \DateTimeImmutable();
    }

    public function isExpiringSoon(int $days = 30): bool
    {
        if (null === $this->expiredAt) {
            return false;
        }

        return $this->expiredAt < (new \DateTimeImmutable())->modify('+'.$days.' days');
    }
}
