<?php

declare(strict_types=1);

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

final class CreatePhoneSmsLog 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('phone_sms_log');
        if (!$exists) {
            $table = $this->table('phone_sms_log', ['comment' => '手机短信日志']);
            $table->addColumn('player_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'null' => false, 'default' => 0, 'comment' => '玩家id'])
                ->addColumn('phone', 'string', ['limit' => 20, 'null' => false, 'default' => '', 'comment' => '手机号'])
                ->addColumn('code', 'string', ['limit' => 10, 'null' => false, 'default' => '', 'comment' => '驗證碼'])
                ->addColumn('type', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'comment' => '类型 1 登录 2注册 3修改密码'])
                ->addColumn('expire_time', 'datetime', ['default' => null, 'comment' => '過期時間'])
                ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 0, 'comment' => '狀態，0=失敗, 1=成功'])
                ->addColumn('send_times', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 1, 'comment' => '發送次數'])
                ->addColumn('response', 'text', ['limit' => MysqlAdapter::TEXT_REGULAR, 'comment' => '簡訊商回傳內容'])
                ->addColumn('uid', 'string', ['limit' => 40, 'null' => false, 'default' => '', 'comment' => '短信发送编号'])
                ->addColumn('created_at', 'timestamp', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'timestamp', ['comment' => '更新时间'])
                ->addIndex(['player_id'], ['name' => 'idx_player_id'])
                ->addIndex(['phone'], ['name' => 'idx_phone'])
                ->create();
        }
    }
}
