<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260603120000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute le stockage des versions metier exploitables.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE IF NOT EXISTS business_version (id UUID NOT NULL, object_class VARCHAR(255) NOT NULL, object_id VARCHAR(128) NOT NULL, scope VARCHAR(64) NOT NULL, version INT NOT NULL, status VARCHAR(32) NOT NULL, snapshot JSON NOT NULL, metadata JSON NOT NULL, created_by VARCHAR(255) DEFAULT NULL, valid_from TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, valid_to TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, published_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS business_version_object_version_unique ON business_version (object_class, object_id, scope, version)');
        $this->addSql('CREATE INDEX IF NOT EXISTS business_version_object_idx ON business_version (object_class, object_id, scope)');
        $this->addSql('CREATE INDEX IF NOT EXISTS business_version_status_idx ON business_version (status)');
        $this->addSql('COMMENT ON COLUMN business_version.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN business_version.valid_from IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN business_version.valid_to IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN business_version.published_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN business_version.created_at IS \'(DC2Type:datetime_immutable)\'');
    }

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