<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20260326165659 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Migrate manga.genres column from PHP-serialized array to JSON';
    }

    public function preUp(Schema $schema): void
    {
        // Convert existing PHP-serialized data to JSON before changing the column type
        $rows = $this->connection->fetchAllAssociative('SELECT id, genres FROM manga WHERE genres IS NOT NULL');
        foreach ($rows as $row) {
            $raw = $row['genres'];
            // Skip if already valid JSON
            json_decode($raw);
            if (json_last_error() === JSON_ERROR_NONE) {
                continue;
            }
            // Unserialize PHP format and re-encode as JSON
            $value = @unserialize($raw);
            if ($value === false && $raw !== 'b:0;') {
                $value = [];
            }
            $this->connection->executeStatement(
                'UPDATE manga SET genres = :json WHERE id = :id',
                ['json' => json_encode($value), 'id' => $row['id']]
            );
        }
    }

    public function up(Schema $schema): void
    {
        $this->addSql('COMMENT ON COLUMN api_token.expires_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN content_source.health_last_tested_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN failed_job.failed_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN job.created_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN job.started_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN job.completed_at IS \'\'');
        $this->addSql('ALTER TABLE manga ALTER genres TYPE JSON USING genres::json');
        $this->addSql('COMMENT ON COLUMN manga.genres IS \'\'');
        $this->addSql('COMMENT ON COLUMN manga.created_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN manga.last_monitoring_check IS \'\'');
        $this->addSql('COMMENT ON COLUMN manga_preferred_sources.created_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN manga_preferred_sources.updated_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN source.created_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN source.updated_at IS \'\'');
        $this->addSql('DROP INDEX idx_75ea56e0e3bd61ce');
        $this->addSql('DROP INDEX idx_75ea56e0fb7336f0');
        $this->addSql('DROP INDEX idx_75ea56e016ba31db');
        $this->addSql('ALTER TABLE messenger_messages ALTER id DROP DEFAULT');
        $this->addSql('ALTER TABLE messenger_messages ALTER id ADD GENERATED BY DEFAULT AS IDENTITY');
        $this->addSql('COMMENT ON COLUMN messenger_messages.created_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN messenger_messages.available_at IS \'\'');
        $this->addSql('COMMENT ON COLUMN messenger_messages.delivered_at IS \'\'');
        $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0E3BD61CE16BA31DBBF396750 ON messenger_messages (queue_name, available_at, delivered_at, id)');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('COMMENT ON COLUMN api_token.expires_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN content_source.health_last_tested_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN failed_job.failed_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN job.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN job.started_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN job.completed_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('ALTER TABLE manga ALTER genres TYPE TEXT');
        $this->addSql('COMMENT ON COLUMN manga.genres IS \'(DC2Type:array)\'');
        $this->addSql('COMMENT ON COLUMN manga.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN manga.last_monitoring_check IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN manga_preferred_sources.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN manga_preferred_sources.updated_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('DROP INDEX IDX_75EA56E0FB7336F0E3BD61CE16BA31DBBF396750');
        $this->addSql('ALTER TABLE messenger_messages ALTER id SET DEFAULT nextval(\'messenger_messages_id_seq\'::regclass)');
        $this->addSql('ALTER TABLE messenger_messages ALTER id DROP IDENTITY');
        $this->addSql('COMMENT ON COLUMN messenger_messages.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN messenger_messages.available_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN messenger_messages.delivered_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('CREATE INDEX idx_75ea56e0e3bd61ce ON messenger_messages (available_at)');
        $this->addSql('CREATE INDEX idx_75ea56e0fb7336f0 ON messenger_messages (queue_name)');
        $this->addSql('CREATE INDEX idx_75ea56e016ba31db ON messenger_messages (delivered_at)');
        $this->addSql('COMMENT ON COLUMN source.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN source.updated_at IS \'(DC2Type:datetime_immutable)\'');
    }
}
