<?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\Bridge\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260722210000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la configuration du contrôle d’âge légal et ses règles par pays.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("CREATE TABLE site_age_verification_config (id UUID NOT NULL, enabled BOOLEAN DEFAULT false NOT NULL, default_country_code VARCHAR(2) DEFAULT 'FR' NOT NULL, default_minimum_age SMALLINT DEFAULT 21 NOT NULL, remember_for_days SMALLINT DEFAULT 30 NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))");
        $this->addSql('CREATE TABLE site_age_verification_country_rule (id UUID NOT NULL, config_id UUID NOT NULL, country_code VARCHAR(2) NOT NULL, minimum_age SMALLINT NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_AGE_VERIFICATION_COUNTRY_CONFIG ON site_age_verification_country_rule (config_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_age_verification_country ON site_age_verification_country_rule (config_id, country_code)');
        $this->addSql('ALTER TABLE site_age_verification_country_rule ADD CONSTRAINT FK_AGE_VERIFICATION_COUNTRY_CONFIG FOREIGN KEY (config_id) REFERENCES site_age_verification_config (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE site_age_verification_country_rule');
        $this->addSql('DROP TABLE site_age_verification_config');
    }
}
