<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddExternalParticipantFieldsToEventCharges extends Migration
{
    public function up()
    {
        $fields = [
            'external_firstname' => [
                'type' => 'VARCHAR',
                'constraint' => 150,
                'null' => true,
                'after' => 'event_payment_id',
            ],
            'external_lastname' => [
                'type' => 'VARCHAR',
                'constraint' => 150,
                'null' => true,
            ],
            'external_note' => [
                'type' => 'VARCHAR',
                'constraint' => 254,
                'null' => true,
            ],
        ];

        if (! $this->db->fieldExists('external_firstname', 'event_charges')) {
            $this->forge->addColumn('event_charges', $fields);
        }
    }

    public function down()
    {
        foreach (['external_firstname', 'external_lastname', 'external_note'] as $field) {
            if ($this->db->fieldExists($field, 'event_charges')) {
                $this->forge->dropColumn('event_charges', $field);
            }
        }
    }
}
