<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260327143000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute ordered_at sur cart_cart_checkout pour distinguer les commandes offline envoyees dans le compte client.';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf(
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
            'Cette migration ne supporte que PostgreSQL.'
        );

        $this->addSql('ALTER TABLE cart_cart_checkout ADD ordered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql(<<<'SQL'
            UPDATE cart_cart_checkout checkout
            SET ordered_at = checkout.updated_at
            FROM cart_cart cart, cart_payment_method payment_method
            WHERE checkout.cart_cart_id = cart.id
              AND payment_method.id = checkout.payment_method_id
              AND checkout.disabled = FALSE
              AND checkout.payed_at IS NULL
              AND checkout.ordered_at IS NULL
              AND payment_method.payment_online = FALSE
              AND cart.cgv = TRUE
        SQL);
    }

    public function down(Schema $schema): void
    {
        $this->abortIf(
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
            'Cette migration ne supporte que PostgreSQL.'
        );

        $this->addSql('ALTER TABLE cart_cart_checkout DROP ordered_at');
    }
}
