<?php

declare(strict_types=1);

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

final class CreatePlayerRechargeRecord 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('player_recharge_record');
        if (!$exists) {
            $table = $this->table('player_recharge_record', ['comment' => '玩家充值记录']);
            $table->addColumn('player_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '玩家id'])
                ->addColumn('department_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'null' => false, 'default' => 0, 'comment' => '部门/渠道id'])
                ->addColumn('talk_user_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'default' => null, 'comment' => '来聊聊账号id'])
                ->addColumn('setting_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'default' => null, 'comment' => '充值账号配置id'])
                ->addColumn('tradeno', 'string', ['limit' => 100, 'null' => false, 'default' => '', 'comment' => '單號'])
                ->addColumn('status', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 0, 'comment' => '狀態, 0=充值中 1=待审核, 2=成功, 3=失败, 4=取消(用户取消) 5=拒绝 6=已关闭(后端取消任务)'])
                ->addColumn('type', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'null' => false, 'default' => 1, 'comment' => '1来聊聊充值(第三方充值) 2渠道充值(官方充值) 3币商转入'])
                ->addColumn('player_name', 'string', ['limit' => 20, 'null' => false, 'default' => null, 'comment' => '玩家姓名'])
                ->addColumn('player_phone', 'string', ['limit' => 20, 'null' => false, 'default' => null, 'comment' => '玩家手機號碼'])
                ->addColumn('point', 'decimal', ['precision' => 14, 'scale' => 2, 'null' => false, 'default' => 0, 'comment' => '充值点数'])
                ->addColumn('money', 'decimal', ['precision' => 14, 'scale' => 2, 'null' => false, 'default' => 0, 'comment' => '充值金額'])
                ->addColumn('inmoney', 'decimal', ['precision' => 14, 'scale' => 2, 'null' => false, 'default' => 0, 'comment' => '實際充值金額'])
                ->addColumn('currency', 'string', ['limit' => 10, 'null' => false, 'default' => null, 'comment' => '幣種'])
                ->addColumn('user_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => true, 'null' => false, 'default' => 0, 'comment' => '管理员id'])
                ->addColumn('user_name', 'string', ['limit' => 50, 'null' => false, 'default' => '', 'comment' => '管理员'])
                ->addColumn('player_tag', 'string', ['limit' => 100, 'null' => false, 'default' => '', 'comment' => '玩家標籤'])
                ->addColumn('remark', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '備註'])
                ->addColumn('reject_reason', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '拒绝原因'])
                ->addColumn('notify_result', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '異步通知回傳結果'])
                ->addColumn('certificate', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '付款凭证'])
                ->addColumn('finish_time', 'datetime', ['comment' => '入款成功时间'])
                ->addColumn('cancel_time', 'datetime', ['comment' => '取消時間'])
                ->addColumn('created_at', 'timestamp', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'timestamp', ['comment' => '更新时间'])
                ->addIndex(['tradeno'], ['name' => 'uni_tradeno', 'unique' => true])
                ->addIndex(['status'], ['name' => 'idx_status'])
                ->addIndex(['type'], ['name' => 'idx_type'])
                ->addIndex(['player_name'], ['name' => 'idx_player_name'])
                ->addIndex(['player_id'], ['name' => 'idx_player_id'])
                ->addIndex(['talk_user_id'], ['name' => 'idx_talk_user_id'])
                ->addIndex(['department_id'], ['name' => 'idx_department_id'])
                ->create();
        }
    }
}
