<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260221000100 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Creates knowledge_tag and document_tag tables (MariaDB compatible)';
    }

    public function up(Schema $schema): void
    {
        // KNOWLEDGE_TAG TABLE (matches Tag entity)
        $this->addSql("
            CREATE TABLE knowledge_tag (
                id BINARY(16) NOT NULL,
                slug VARCHAR(120) NOT NULL,
                label VARCHAR(180) NOT NULL,
                description TEXT DEFAULT NULL,
                created_at DATETIME NOT NULL,
                PRIMARY KEY(id),
                UNIQUE INDEX uniq_knowledge_tag_slug (slug),
                INDEX idx_knowledge_tag_slug (slug),
                INDEX idx_knowledge_tag_label (label)
            ) ENGINE=InnoDB
              DEFAULT CHARSET=utf8mb4
              COLLATE=utf8mb4_unicode_ci
        ");

        // DOCUMENT_TAG TABLE
        $this->addSql("
            CREATE TABLE document_tag (
                document_id BINARY(16) NOT NULL,
                tag_id BINARY(16) NOT NULL,
                PRIMARY KEY(document_id, tag_id),
                KEY IDX_DOCUMENT_TAG_DOCUMENT (document_id),
                KEY IDX_DOCUMENT_TAG_TAG (tag_id),
                CONSTRAINT FK_DOCUMENT_TAG_DOCUMENT
                    FOREIGN KEY (document_id)
                    REFERENCES document (id)
                    ON DELETE CASCADE,
                CONSTRAINT FK_DOCUMENT_TAG_TAG
                    FOREIGN KEY (tag_id)
                    REFERENCES knowledge_tag (id)
                    ON DELETE CASCADE
            ) ENGINE=InnoDB
              DEFAULT CHARSET=utf8mb4
              COLLATE=utf8mb4_unicode_ci
        ");
    }

    public function down(Schema $schema): void
    {
        $this->addSql("DROP TABLE document_tag");
        $this->addSql("DROP TABLE knowledge_tag");
    }
}