<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddUniqueIndexesToMenuTables extends AbstractMigration
{
    public function up(): void
    {

        $this->table('menus')
            ->removeIndex(['name'])
            ->addIndex(
                ['name'],
                [
                    'unique' => true,
                    'name' => 'unique_menu_name',
                ]
            )
            ->update();

        $this->table('menu_links')
            ->removeIndex(['title'])
            ->addIndex(
                ['title', 'mid', 'parentId'],
                [
                    'unique' => true,
                    'name' => 'unique_menu_link_title_mid_parent',
                ]
            )
            ->update();
    }

    public function down(): void
    {
        $this->table('menus')
            ->removeIndexByName('unique_menu_name')
            ->addIndex(['name'])
            ->update();

        $this->table('menu_links')
            ->removeIndexByName('unique_menu_link_title_mid_parent')
            ->addIndex(['title', 'mid', 'parentId'])
            ->update();
    }
}
