<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260510093000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute le socle du page builder pour les pages et sections éditoriales.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE page_builder_page (id UUID NOT NULL, code VARCHAR(64) NOT NULL, name VARCHAR(255) NOT NULL, route_name VARCHAR(255) DEFAULT NULL, system BOOLEAN DEFAULT false NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_6D3A6FF277153098 ON page_builder_page (code)');
        $this->addSql('COMMENT ON COLUMN page_builder_page.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page.updated_at IS \'(DC2Type:datetime_immutable)\'');

        $this->addSql('CREATE TABLE page_builder_page_section (id UUID NOT NULL, page_id UUID NOT NULL, image_id UUID DEFAULT NULL, type VARCHAR(64) NOT NULL, admin_label VARCHAR(255) DEFAULT NULL, settings JSON DEFAULT NULL, disabled BOOLEAN DEFAULT false NOT NULL, position INT DEFAULT 0 NOT NULL, published_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, un_published_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_18F7AF9CC4663E4 ON page_builder_page_section (page_id)');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_18F7AF93DA5256D ON page_builder_page_section (image_id)');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section.page_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section.image_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section.updated_at IS \'(DC2Type:datetime_immutable)\'');

        $this->addSql('CREATE TABLE page_builder_page_section_translation (id UUID NOT NULL, translatable_id UUID DEFAULT NULL, eyebrow VARCHAR(255) DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, body TEXT DEFAULT NULL, primary_cta_label VARCHAR(255) DEFAULT NULL, primary_cta_url TEXT DEFAULT NULL, secondary_cta_label VARCHAR(255) DEFAULT NULL, secondary_cta_url TEXT DEFAULT NULL, quote_source VARCHAR(255) DEFAULT NULL, quote_role VARCHAR(255) DEFAULT NULL, locale VARCHAR(5) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_40507A5C2C2AC5D3 ON page_builder_page_section_translation (translatable_id)');
        $this->addSql('CREATE UNIQUE INDEX page_builder_page_section_translation_unique_translation ON page_builder_page_section_translation (translatable_id, locale)');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section_translation.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN page_builder_page_section_translation.translatable_id IS \'(DC2Type:uuid)\'');

        $this->addSql('ALTER TABLE page_builder_page_section ADD CONSTRAINT FK_18F7AF9CC4663E4 FOREIGN KEY (page_id) REFERENCES page_builder_page (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE page_builder_page_section ADD CONSTRAINT FK_18F7AF93DA5256D FOREIGN KEY (image_id) REFERENCES media_media (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE page_builder_page_section_translation ADD CONSTRAINT FK_40507A5C2C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES page_builder_page_section (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE page_builder_page_section_translation DROP CONSTRAINT FK_40507A5C2C2AC5D3');
        $this->addSql('ALTER TABLE page_builder_page_section DROP CONSTRAINT FK_18F7AF9CC4663E4');
        $this->addSql('ALTER TABLE page_builder_page_section DROP CONSTRAINT FK_18F7AF93DA5256D');
        $this->addSql('DROP TABLE page_builder_page');
        $this->addSql('DROP TABLE page_builder_page_section');
        $this->addSql('DROP TABLE page_builder_page_section_translation');
    }
}
