<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Programs extends Migration
{
    public function up()
    {
        // Tabel Programs
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'kode_program' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
            ],
            'nama_program' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
            ],
            'opd_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->addForeignKey('opd_id', 'opd', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('programs');

        // Tabel Kegiatan
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'program_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'kode_kegiatan' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
            ],
            'nama_kegiatan' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('program_id', 'programs', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('kegiatan');

        // Tabel Sub Kegiatan
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'kegiatan_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'kode_sub_kegiatan' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
            ],
            'nama_sub_kegiatan' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
            ],
            'target_kinerja' => [
                'type'       => 'TEXT',
                'null'       => true,
            ],
            'satuan' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
                'null'       => true,
            ],
            'pagu_anggaran' => [
                'type'       => 'DECIMAL',
                'constraint' => '15,2',
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('kegiatan_id', 'kegiatan', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('sub_kegiatan');
    }

    public function down()
    {
        $this->forge->dropTable('sub_kegiatan');
        $this->forge->dropTable('kegiatan');
        $this->forge->dropTable('programs');
    }
}
