<?php

declare(strict_types=1);

/*
 * 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\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260722231000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Préserve le fuseau des dates techniques du rattachement Commerce.';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf(!$this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform, 'Cette migration nécessite PostgreSQL.');

        foreach (['enrollment_token_expires_at', 'enrolled_at', 'authorization_expires_at'] as $column) {
            $this->addSql(sprintf(
                "ALTER TABLE commerce_catalog_configuration ALTER %s TYPE TIMESTAMP(0) WITH TIME ZONE USING %s AT TIME ZONE 'UTC'",
                $column,
                $column,
            ));
            $this->addSql(sprintf(
                "COMMENT ON COLUMN commerce_catalog_configuration.%s IS '(DC2Type:datetimetz_immutable)'",
                $column,
            ));
        }
    }

    public function down(Schema $schema): void
    {
        $this->abortIf(!$this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform, 'Cette migration nécessite PostgreSQL.');

        foreach (['enrollment_token_expires_at', 'enrolled_at', 'authorization_expires_at'] as $column) {
            $this->addSql(sprintf(
                "ALTER TABLE commerce_catalog_configuration ALTER %s TYPE TIMESTAMP(0) WITHOUT TIME ZONE USING %s AT TIME ZONE 'UTC'",
                $column,
                $column,
            ));
            $this->addSql(sprintf(
                "COMMENT ON COLUMN commerce_catalog_configuration.%s IS '(DC2Type:datetime_immutable)'",
                $column,
            ));
        }
    }
}
