<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateLearningDevicesTables extends Migration
{
    public function up()
    {
        // 1. ai_kb_cp (Master CP Lokal)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'subject_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => true,
            ],
            'subject_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
            ],
            'phase' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
            ],
            'content' => [
                'type' => 'TEXT',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'deleted_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('subject_id', 'subjects', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('ai_kb_cp', true);

        // 2. learning_devices (Perangkat Pembelajaran)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'user_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'academic_year_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'grade_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'school_subject_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'phase' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
            ],
            'school_type' => [
                'type'       => 'ENUM',
                'constraint' => ['umum', 'madrasah'],
                'default'    => 'umum',
            ],
            'cp_content' => [
                'type' => 'TEXT',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'deleted_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_year_id', 'academic_years', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('grade_id', 'grades', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('school_subject_id', 'school_subjects', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('learning_devices', true);

        // 3. learning_device_tps (Tujuan Pembelajaran & ATP)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'learning_device_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'code' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'description' => [
                'type' => 'TEXT',
            ],
            'allocated_hours' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 0,
            ],
            'sort_order' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('learning_device_id', 'learning_devices', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('learning_device_tps', true);

        // 4. learning_device_prota (Program Tahunan)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'learning_device_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'tp_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'semester' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 1,
            ],
            'allocated_weeks' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 0,
            ],
            'allocated_hours' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 0,
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('learning_device_id', 'learning_devices', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('tp_id', 'learning_device_tps', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('learning_device_prota', true);

        // 5. learning_device_promes (Program Semester)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'learning_device_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'tp_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'month' => [
                'type'       => 'TINYINT',
                'constraint' => 2,
            ],
            'week_number' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
            ],
            'allocated_hours' => [
                'type'       => 'INT',
                'constraint' => 5,
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('learning_device_id', 'learning_devices', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('tp_id', 'learning_device_tps', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('learning_device_promes', true);

        // 6. learning_device_moduls (Modul Ajar)
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'learning_device_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'tp_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
            ],
            'topic' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
            'identity' => [
                'type' => 'JSON',
            ],
            'core_components' => [
                'type' => 'JSON',
            ],
            'differentiation' => [
                'type' => 'JSON',
            ],
            'assessment' => [
                'type' => 'JSON',
            ],
            'follow_up' => [
                'type' => 'JSON',
            ],
            'reflection' => [
                'type' => 'JSON',
            ],
            'character_integration' => [
                'type' => 'JSON',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('learning_device_id', 'learning_devices', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('tp_id', 'learning_device_tps', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('learning_device_moduls', true);
    }

    public function down()
    {
        $this->forge->dropTable('learning_device_moduls', true);
        $this->forge->dropTable('learning_device_promes', true);
        $this->forge->dropTable('learning_device_prota', true);
        $this->forge->dropTable('learning_device_tps', true);
        $this->forge->dropTable('learning_devices', true);
        $this->forge->dropTable('ai_kb_cp', true);
    }
}
