<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260722180000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute l’outbox durable d’enrôlement automatique d’un catalogue VitiCore dans le hub central.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("CREATE TABLE commerce_catalog_enrollment_request (id BIGSERIAL NOT NULL, request_id UUID NOT NULL, instance_id UUID DEFAULT NULL, configuration_fingerprint VARCHAR(64) DEFAULT NULL, status VARCHAR(32) NOT NULL, delivery_attempts INT DEFAULT 0 NOT NULL, lock_version INT DEFAULT 1 NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, last_queued_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, last_attempt_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, registered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, permanently_failed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, source_id VARCHAR(255) DEFAULT NULL, webhook_url TEXT DEFAULT NULL, hub_status VARCHAR(64) DEFAULT NULL, last_response JSON DEFAULT NULL, last_error TEXT DEFAULT NULL, PRIMARY KEY(id))");
        $this->addSql('CREATE UNIQUE INDEX commerce_catalog_enrollment_request_id_uniq ON commerce_catalog_enrollment_request (request_id)');
        $this->addSql('CREATE INDEX commerce_catalog_enrollment_delivery_idx ON commerce_catalog_enrollment_request (status, last_queued_at)');
        $this->addSql('CREATE INDEX commerce_catalog_enrollment_fingerprint_idx ON commerce_catalog_enrollment_request (configuration_fingerprint)');
        $this->addSql("COMMENT ON COLUMN commerce_catalog_enrollment_request.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_enrollment_request.last_queued_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_enrollment_request.last_attempt_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_enrollment_request.registered_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_enrollment_request.permanently_failed_at IS '(DC2Type:datetime_immutable)'");
    }

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