{#
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         3.0.0
 * @license       https://www.opensource.org/licenses/mit-license.php MIT License
 */
#}
{% set wantedOptions = {'length': '', 'limit': '', 'default': '', 'unsigned': '', 'null': '', 'comment': '', 'autoIncrement': '', 'precision': '', 'scale': ''} %}
{% set tableMethod = Migration.tableMethod(action) %}
{% set columnMethod = Migration.columnMethod(action) %}
{% set indexMethod = Migration.indexMethod(action) %}
<?php
declare(strict_types=1);

{% if backend == "builtin" %}
use Migrations\BaseMigration;

class {{ name }} extends BaseMigration
{% else %}
use Migrations\AbstractMigration;

class {{ name }} extends AbstractMigration
{% endif %}
{
{% if tableMethod == 'create' and columns['primaryKey'] %}
    public bool $autoId = false;

{% endif %}
    /**
     * Change Method.
     *
     * More information on this method is available here:
{% if backend == "builtin" %}
     * https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
{% else %}
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
{% endif %}
     *
     * @return void
     */
    public function change(): void
    {
{% for table in tables %}
        $table = $this->table('{{ table }}');
    {%~ if tableMethod != 'drop' %}
        {%~ if columnMethod == 'removeColumn' %}
            {%~ for column, config in columns['fields'] %}
        $table->{{ columnMethod }}('{{ column }}');
            {%~ endfor %}
            {%~ for column, config in columns['indexes'] %}
        $table->{{ indexMethod }}([{{
            Migration.stringifyList(config['columns'])  | raw
        }}]);
            {%~ endfor %}
    {%~ else %}
        {%~ for column, config in columns['fields'] %}
        $table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{
            Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw
        }}]);
        {%~ endfor %}
        {%~ for column, config in columns['indexes'] %}
        $table->{{ indexMethod }}([{{
             Migration.stringifyList(config['columns'], {'indent': 3}) | raw }}
            ], [{{
                Migration.stringifyList(config['options'], {'indent': 3})  | raw
        }}]);
        {%~ endfor %}
        {%~ if tableMethod == 'create' and columns['primaryKey'] is not empty %}
        $table->addPrimaryKey([{{
            Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw
        }}]);
        {%~ endif %}
        {%~ if constraints is defined and constraints is not empty %}
            {%~ for constraintName, constraint in constraints %}
                {%~ if constraint['type'] == 'foreign' %}
                    {%~ set columnsList = '\'' ~ constraint['columns'][0] ~ '\'' %}
                    {%~ if constraint['columns']|length > 1 %}
                        {%~ set columnsList = '[' ~ Migration.stringifyList(constraint['columns'], {'indent': 3}) ~ ']' %}
                    {%~ endif %}
                    {%~ if constraint['references'][1] is iterable %}
                        {%~ set columnsReference = '[' ~ Migration.stringifyList(constraint['references'][1], {'indent': 3}) ~ ']' %}
                    {%~ else %}
                        {%~ set columnsReference = '\'' ~ constraint['references'][1] ~ '\'' %}
                    {%~ endif %}
                    {%~ if backend == 'builtin' %}
        $table->addForeignKey(
            $this->foreignKey({{ columnsList | raw }})
                ->setReferencedTable('{{ constraint['references'][0] }}')
                ->setReferencedColumns({{ columnsReference | raw }})
                ->setOnDelete('{{ Migration.formatConstraintAction(constraint['delete']) | raw }}')
                ->setOnUpdate('{{ Migration.formatConstraintAction(constraint['update']) | raw }}')
                ->setName('{{ constraintName }}')
        );
                    {%~ else %}
        $table->addForeignKey(
            {{ columnsList  | raw }},
            '{{ constraint['references'][0] }}',
            {{ columnsReference | raw }},
            [
                'update' => '{{ Migration.formatConstraintAction(constraint['update']) | raw }}',
                'delete' => '{{ Migration.formatConstraintAction(constraint['delete']) | raw }}',
                'constraint' => '{{ constraintName }}'
            ]
        );
                    {%~ endif %}
                {%~ endif %}
            {%~ endfor %}
        {%~ endif %}
    {%~ endif %}
{% endif %}
        $table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %};
{% endfor %}
    }
}
