<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Create_yem_transactions_table extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'hash' => [
                'type'       => 'VARCHAR',
                'constraint' => 66,
                'comment'    => 'Transaction hash (0x-prefixed)',
            ],
            'from_uid' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'to_uid' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'value' => [
                'type'       => 'DECIMAL',
                'constraint' => '20,2',
                'comment'    => 'Amount in YEM (decimal)',
            ],
            'reason' => [
                'type' => 'VARCHAR',
                'constraint' => 500,
                'null' => true,
            ],
            'asset' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'YEM',
            ],
            'from_curr' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'null'       => true,
            ],
            'to_curr' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'null'       => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'pending',
                'comment'    => 'pending, confirmed, failed, not_found',
            ],
            'status_updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('hash');
        $this->forge->addKey('status');
        $this->forge->addKey('created_at');
        $this->forge->addKey(['from_uid', 'to_uid']);
        $this->forge->createTable('yem_transactions');
    }

    public function down()
    {
        $this->forge->dropTable('yem_transactions');
    }
}
