<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddServicePaymentTracking extends Migration
{
    public function up()
    {
        $this->forge->addColumn('service', [
            'status_pembayaran' => [
                'type'       => 'ENUM',
                'constraint' => ['piutang', 'lunas'],
                'default'    => 'piutang',
                'after'      => 'total_service',
            ],
        ]);

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'service_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,
                'unsigned'   => true,
                '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('service_id');
        $this->forge->addKey('tanggal_pembayaran');
        $this->forge->addForeignKey('service_id', 'service', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('user_id', 'user', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('service_pembayaran');
    }

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