<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Stores the purchaser's real phone for FinFlo (and similar) flows where
 * transactions.msisdn is later set to finflo-TX-xxx for reference lookup.
 */
class AddCustomerMsisdnToTransactions extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('customer_msisdn', 'transactions')) {
            $this->forge->addColumn('transactions', [
                'customer_msisdn' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 80,
                    'null'       => true,
                    'after'      => 'msisdn',
                ],
            ]);
            $this->db->query('CREATE INDEX idx_transactions_customer_msisdn ON transactions (customer_msisdn)');
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('customer_msisdn', 'transactions')) {
            $this->db->query('DROP INDEX idx_transactions_customer_msisdn ON transactions');
            $this->forge->dropColumn('transactions', 'customer_msisdn');
        }
    }
}
