<?php

declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;

final class CreateCurrency extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $exists = $this->hasTable('currency');
        if (!$exists) {
            $table = $this->table('currency', ['comment' => '货币配置']);
            $table->addColumn('name', 'string', ['limit' => 120, 'null' => false, 'default' => '', 'comment' => '货币名称'])
                ->addColumn('identifying', 'string', ['limit' => 80, 'null' => false, 'default' => '', 'comment' => '货币标识'])
                ->addColumn('ratio', 'decimal', ['precision' => 14, 'scale' => 4, 'signed' => true, 'null' => false, 'default' => 0, 'comment' => '1货币 = 点数'])
                ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 1, 'comment' => '狀態，0=停用, 1=啟用'])
                ->addColumn('admin_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '管理员id'])
                ->addColumn('created_at', 'datetime', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'datetime', ['comment' => '更新时间'])
                ->addColumn('deleted_at', 'datetime', ['comment' => '删除时间'])
                ->addIndex(['identifying'], ['name' => 'uni_identifying', 'unique' => true])
                ->create();
        }
    }
}
