<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddPembelianPaymentTracking extends Migration
{
    public function up()
    {
        $this->forge->addColumn('pembelian', [
            'status_pembayaran' => [
                'type'       => 'ENUM',
                'constraint' => ['hutang', 'lunas'],
                'default'    => 'hutang',
                'after'      => 'catatan',
            ],
        ]);

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'pembelian_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'tanggal_pembayaran' => [
                'type' => 'DATE',
            ],
            'jumlah' => [
                'type'       => 'BIGINT',
                'constraint' => 20,
                'unsigned'   => true,
                'default'    => 0,
            ],
            'is_pembayaran_awal' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
            ],
            'catatan' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'user_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('pembelian_id');
        $this->forge->addKey('tanggal_pembayaran');
        $this->forge->addForeignKey('pembelian_id', 'pembelian', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('user_id', 'user', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('pembelian_pembayaran');
    }

    public function down()
    {
        $this->forge->dropTable('pembelian_pembayaran');
        $this->forge->dropColumn('pembelian', 'status_pembayaran');
    }
}
