<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTeachingJournalsTable extends Migration
{
    public function up()
    {
        // ── teaching_journals ─────────────────────────────────────
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'teacher_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'academic_period_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'classroom_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'school_subject_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'meeting_date' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'start_period' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'end_period' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'meeting_number' => [
                'type'       => 'INT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'topic' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'tp_ids' => [
                'type' => 'JSON',
                'null' => true,
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'total_students' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'total_present' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'total_sick' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'total_permit' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'total_absent' => [
                'type'       => 'TINYINT',
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'is_final' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'null'       => false,
                'default'    => 0,
            ],
            '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('teacher_id', 'teachers', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('classroom_id', 'classrooms', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('school_subject_id', 'school_subjects', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addKey('meeting_date');
        $this->forge->addKey(['teacher_id', 'academic_period_id', 'classroom_id', 'school_subject_id']);
        $this->forge->createTable('teaching_journals', true);

        // ── journal_attendances ──────────────────────────────────
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'journal_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'student_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'status' => [
                'type'       => "ENUM('Sakit','Izin','Alpha')",
                'null'       => false,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('journal_id', 'teaching_journals', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addUniqueKey(['journal_id', 'student_id']);
        $this->forge->createTable('journal_attendances', true);
    }

    public function down()
    {
        $this->forge->dropTable('journal_attendances');
        $this->forge->dropTable('teaching_journals');
    }
}
