<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260722160000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute l’outbox transactionnelle du catalogue Commerce et le suivi des notifications webhook.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("CREATE TABLE commerce_catalog_change (cursor BIGSERIAL NOT NULL, event_id UUID NOT NULL, instance_id UUID NOT NULL, type VARCHAR(64) NOT NULL, resource_type VARCHAR(32) NOT NULL, resource_id VARCHAR(255) DEFAULT NULL, occurred_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivery_status VARCHAR(32) NOT NULL, delivery_attempts INT DEFAULT 0 NOT NULL, last_queued_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, last_attempt_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, permanently_failed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, last_error TEXT DEFAULT NULL, PRIMARY KEY(cursor))");
        $this->addSql('CREATE UNIQUE INDEX commerce_catalog_change_event_id_uniq ON commerce_catalog_change (event_id)');
        $this->addSql('CREATE INDEX commerce_catalog_change_delivery_idx ON commerce_catalog_change (delivery_status, last_queued_at)');
        $this->addSql('CREATE INDEX commerce_catalog_change_occurred_at_idx ON commerce_catalog_change (occurred_at)');
        $this->addSql("COMMENT ON COLUMN commerce_catalog_change.occurred_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_change.last_queued_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_change.last_attempt_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_change.delivered_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_change.permanently_failed_at IS '(DC2Type:datetime_immutable)'");
    }

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