<?php

declare(strict_types=1);

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

final class CreatePlayerDeliveryRecord 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_delivery_record');
        if (!$exists) {
            $table = $this->table('player_delivery_record', ['comment' => '玩家变账记录']);
            $table->addColumn('player_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'null' => false, 'comment' => '玩家id'])
                ->addColumn('department_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'null' => false, 'comment' => '渠道/部门id'])
                ->addColumn('code', 'string', ['limit' => 50, 'null' => false, 'default' => '', 'comment' => '機台編號'])
                ->addColumn('target', 'string', ['limit' => 50, 'null' => false, 'default' => '', 'comment' => '交易資料表'])
                ->addColumn('target_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'null' => false, 'default' => 0, 'comment' => '資料id'])
                ->addColumn('type', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'signed' => false, 'null' => false, 'comment' => '类型'])
                ->addColumn('source', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '来源'])
                ->addColumn('amount', 'decimal', ['precision' => 32, 'scale' => 2, 'null' => false, 'default' => 0, '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('amount_before', 'decimal', ['precision' => 32, 'scale' => 2, 'null' => false, 'default' => 0, 'comment' => '異動前金額'])
                ->addColumn('amount_after', 'decimal', ['precision' => 32, 'scale' => 2, 'null' => false, 'default' => 0, 'comment' => '金额'])
                ->addColumn('tradeno', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '單號'])
                ->addColumn('remark', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => '备注'])
                ->addColumn('created_at', 'timestamp', ['comment' => '创建时间'])
                ->addColumn('updated_at', 'timestamp', ['comment' => '更新时间'])
                ->addIndex(['player_id'], ['name' => 'idx_player_id'])
                ->addIndex(['type'], ['name' => 'idx_type'])
                ->addIndex(['source'], ['name' => 'idx_source'])
                ->addIndex(['amount'], ['name' => 'idx_amount'])
                ->addIndex(['tradeno'], ['name' => 'idx_tradeno'])
                ->addIndex(['department_id'], ['name' => 'idx_department_id'])
                ->create();
        }
    }
}
