<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260519175338 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Align workflow schema with Doctrine naming (indexes, IDENTITY). Drops the partial unique index uniq_workflow_one_default (uniqueness is enforced by UniqueDefaultWorkflowListener at app level).';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER INDEX idx_project_workflow RENAME TO IDX_2FB3D0EE2C7C2CBA');
        $this->addSql('ALTER INDEX idx_task_status_workflow RENAME TO IDX_40A9E1CF2C7C2CBA');
        $this->addSql('DROP INDEX uniq_workflow_one_default');
        $this->addSql('ALTER TABLE workflow ALTER id DROP DEFAULT');
        $this->addSql('ALTER TABLE workflow ALTER id ADD GENERATED BY DEFAULT AS IDENTITY');
        // Aligner la séquence d'identity sur MAX(id) pour éviter le conflit avec les rows déjà insérés par M1
        $this->addSql('SELECT setval(pg_get_serial_sequence(\'workflow\', \'id\'), COALESCE((SELECT MAX(id) FROM workflow), 0) + 1, false)');
        $this->addSql('ALTER INDEX uniq_workflow_name RENAME TO UNIQ_65C598165E237E06');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER INDEX idx_2fb3d0ee2c7c2cba RENAME TO idx_project_workflow');
        $this->addSql('ALTER INDEX idx_40a9e1cf2c7c2cba RENAME TO idx_task_status_workflow');
        $this->addSql('ALTER TABLE workflow ALTER id DROP IDENTITY');
        $this->addSql("ALTER TABLE workflow ALTER id SET DEFAULT nextval('workflow_id_seq'::regclass)");
        $this->addSql('CREATE UNIQUE INDEX uniq_workflow_one_default ON workflow (is_default) WHERE (is_default = true)');
        $this->addSql('ALTER INDEX uniq_65c598165e237e06 RENAME TO uniq_workflow_name');
    }
}
