<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Creates the messenger_messages table for the Doctrine failed transport.
 *
 * Symfony Messenger uses this table to persist messages that have exhausted
 * all retries, enabling later inspection and replay.
 * The queue_name column discriminates between transports (e.g. 'failed').
 */
final class Version20260208100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create messenger_messages table for dead-letter queue';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE IF NOT EXISTS messenger_messages (
                id BIGSERIAL PRIMARY KEY,
                body TEXT NOT NULL,
                headers TEXT NOT NULL,
                queue_name VARCHAR(190) NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                available_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                delivered_at TIMESTAMPTZ DEFAULT NULL
            )
        SQL);

        $this->addSql('CREATE INDEX IF NOT EXISTS idx_messenger_messages_queue_name ON messenger_messages(queue_name)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_messenger_messages_available_at ON messenger_messages(available_at)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_messenger_messages_delivered_at ON messenger_messages(delivered_at)');
    }

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