<?php

declare(strict_types=1);

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

final class CreateExternalApp 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('external_app');
        if (!$exists) {
            $table = $this->table('external_app', ['comment' => '外部应用']);
            $table->addColumn('name', 'string', ['limit' => 120, 'null' => false, 'default' => '', 'comment' => '应用名称'])
                ->addColumn('white_ip', 'text', ['limit' => MysqlAdapter::TEXT_REGULAR, 'comment' => '白化IP'])
                ->addColumn('app_id', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => 'app_id'])
                ->addColumn('app_secret', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => 'app_secret'])
                ->addColumn('user_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '管理员id'])
                ->addColumn('user_name', 'string', ['limit' => 20, 'null' => false, 'default' => '', 'comment' => '管理员名称'])
                ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 1, 'comment' => '狀態，0=停用, 1=啟用'])
                ->addColumn('created_at', 'datetime', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'datetime', ['comment' => '更新时间'])
                ->addColumn('deleted_at', 'datetime', ['comment' => '删除时间'])
                ->addIndex(['name'], ['name' => 'uni_name', 'unique' => true])
                ->addIndex(['user_id'], ['name' => 'idx_user_id'])
                ->create();
        }
    }
}
