<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePaymentNotificationLogs extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('payment_notification_logs')) {
            return;
        }

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'parent_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'invoice_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'school_year' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
            ],
            'period_year' => [
                'type'       => 'SMALLINT',
                'constraint' => 4,
                'null'       => false,
            ],
            'period_month' => [
                'type'       => 'TINYINT',
                'constraint' => 2,
                'null'       => false,
            ],
            'type' => [
                'type'       => 'ENUM',
                'constraint' => ['no_payment', 'installment'],
                'default'    => 'installment',
            ],
            'to_email' => [
                'type'       => 'VARCHAR',
                'constraint' => 191,
                'null'       => true,
            ],
            'cc_email' => [
                'type'       => 'VARCHAR',
                'constraint' => 191,
                'null'       => true,
            ],
            'head_fa_notified' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
            ],
            'subject' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ],
            'body' => [
                'type' => 'MEDIUMTEXT',
                'null' => true,
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['sent', 'failed'],
                'default'    => 'sent',
            ],
            'error_message' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'balance_snapshot' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'sent_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['parent_id']);
        $this->forge->addKey(['invoice_id']);
        $this->forge->addKey(['school_year']);
        $this->forge->addKey(['period_year', 'period_month']);
        // Unique combination to prevent duplicate sends per period/type
        $this->forge->addKey(['parent_id', 'period_year', 'period_month', 'type'], false, true);

        $this->forge->createTable('payment_notification_logs', true);
    }

    public function down()
    {
        if ($this->db->tableExists('payment_notification_logs')) {
            $this->forge->dropTable('payment_notification_logs', true);
        }
    }
}
