<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\AgeVerification\Model;

use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity]
#[ORM\Table(name: 'site_age_verification_country_rule')]
#[ORM\UniqueConstraint(name: 'uniq_age_verification_country', columns: ['config_id', 'country_code'])]
class AgeVerificationCountryRule
{
    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\ManyToOne(targetEntity: AgeVerificationConfig::class, inversedBy: 'countryRules')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?AgeVerificationConfig $config = null;

    #[Assert\Country]
    #[ORM\Column(type: 'string', length: 2)]
    private string $countryCode;

    #[Assert\Range(min: 1, max: 99)]
    #[ORM\Column(type: 'smallint')]
    private int $minimumAge;

    public function __construct(string $countryCode = 'FR', int $minimumAge = 18)
    {
        $this->id = Uuid::uuid4();
        $this->setCountryCode($countryCode);
        $this->minimumAge = $minimumAge;
    }

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

    public function getConfig(): ?AgeVerificationConfig
    {
        return $this->config;
    }

    public function setConfig(?AgeVerificationConfig $config): void
    {
        $this->config = $config;
    }

    public function getCountryCode(): string
    {
        return $this->countryCode;
    }

    public function setCountryCode(string $countryCode): void
    {
        $this->countryCode = mb_strtoupper(mb_trim($countryCode));
    }

    public function getMinimumAge(): int
    {
        return $this->minimumAge;
    }

    public function setMinimumAge(int $minimumAge): void
    {
        $this->minimumAge = $minimumAge;
    }
}
