<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddReferenceColumnsToLetterLogs extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('is_reference', 'letter_logs')) {
            $this->forge->addColumn('letter_logs', [
                'is_reference' => [
                    'type'       => 'TINYINT',
                    'constraint' => 1,
                    'null'       => false,
                    'default'    => 0,
                    'comment'    => '1 = Surat Tugas Menindaklanjuti, 0 = Surat Tugas Mandiri',
                    'after'      => 'budget_source',
                ],
            ]);
        }
        if (!$this->db->fieldExists('reference_officer', 'letter_logs')) {
            $this->forge->addColumn('letter_logs', [
                'reference_officer' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 200,
                    'null'       => true,
                    'default'    => null,
                    'comment'    => 'Jabatan pejabat pengirim surat rujukan',
                    'after'      => 'is_reference',
                ],
            ]);
        }
        if (!$this->db->fieldExists('reference_number', 'letter_logs')) {
            $this->forge->addColumn('letter_logs', [
                'reference_number' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 100,
                    'null'       => true,
                    'default'    => null,
                    'comment'    => 'Nomor surat rujukan',
                    'after'      => 'reference_officer',
                ],
            ]);
        }
        if (!$this->db->fieldExists('reference_date', 'letter_logs')) {
            $this->forge->addColumn('letter_logs', [
                'reference_date' => [
                    'type'    => 'DATE',
                    'null'    => true,
                    'default' => null,
                    'comment' => 'Tanggal surat rujukan',
                    'after'      => 'reference_number',
                ],
            ]);
        }
        if (!$this->db->fieldExists('reference_theme', 'letter_logs')) {
            $this->forge->addColumn('letter_logs', [
                'reference_theme' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 300,
                    'null'       => true,
                    'default'    => null,
                    'comment' => 'Hal/tentang surat rujukan',
                    'after'      => 'reference_date',
                ],
            ]);
        }
    }

    public function down()
    {
        $this->forge->dropColumn('letter_logs', [
            'is_reference',
            'reference_officer',
            'reference_number',
            'reference_date',
            'reference_theme'
        ]);
    }
}
