<?php

namespace App\Database\Migrations\Elearning;

use CodeIgniter\Database\Migration;

class JadwalPelajaran extends Migration
{
    public function up()
    {

        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'mapel_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'kelas_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'tahun_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'startDay' => ['type' => 'enum', 'default' => 2, 'constraint' => "'1','2','3','4','5','6','7'", 'comment' => "1 = Minggu - 7 = Sabtu"],
            'countJam' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 1],
            'startTime' => ['type' => 'time'],
            'endTime' => ['type' => 'time'],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('mapel_id', 'm_mapel', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('kelas_id', 'm_kelas', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('tahun_id', 'tahun_pelajaran', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('jadwal_pelajaran', true);

        $fields = [
            'jadwal_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'rpp_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'rangkuman' => ['type' => 'text', 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('rpp_id', 'rpp', 'id', false, 'SET NULL');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('jadwal_id', 'jadwal_pelajaran', 'id', false, 'CASCADE');
        $this->forge->createTable('agenda_kegiatan', true);
        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'jadwal_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'kompetensi_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'judul' => ['type' => 'varchar', 'constraint' => 255],
            'is_active' => ['type' => 'enum', 'constraint' => "'0','1'", 'default' => '1'],
            'isi' => ['type' => 'text', 'null' => true],
            'file' => ['type' => 'text', 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('jadwal_id', 'jadwal_pelajaran', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('kompetensi_id', 'kompetensi_dasar', 'id', false, 'CASCADE');
        $this->forge->createTable('jadwal_materi', true);

        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'jadwal_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'kompetensi_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'judul' => ['type' => 'varchar', 'constraint' => 255],
            'deskripsi' => ['type' => 'text', 'null' => true],
            'file' => ['type' => 'text', 'null' => true],
            'is_active' => ['type' => 'enum', 'constraint' => "'0','1'", 'default' => '1'],
            'dueDate' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('jadwal_id', 'jadwal_pelajaran', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('kompetensi_id', 'kompetensi_dasar', 'id', false, 'CASCADE');
        $this->forge->createTable('jadwal_tugas', true);

        $fields = [
            'id' => ['type' => 'varchar', 'constraint' => 255],
            'jadwal_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'user_id' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
            'user_type' => ['type' => 'enum', 'constraint' => "'staff','mobile'"],
            'conversation' => ['type' => 'text', 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('jadwal_id', 'jadwal_pelajaran', 'id', false, 'CASCADE');
        $this->forge->createTable('jadwal_diskusi', true);
        $this->db->query("ALTER TABLE `jadwal_diskusi` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");

        $fields = [
            'jadwal_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'keterangan' => ['type' => 'enum', 'constraint' => "'0','1','2','3','4'", 'default' => '0'],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('jadwal_id', 'jadwal_pelajaran', 'id', false, 'CASCADE');
        $this->forge->createTable('jadwal_absensi_siswa', true);

        $fields = [
            'materi_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('materi_id', 'jadwal_materi', 'id', false, 'CASCADE');
        $this->forge->createTable('siswa_viewed_materi', true);
        $fields = [
            'tugas_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'comment' => ['type' => 'text', 'constraint' => 11, 'null' => true],
            'answer' => ['type' => 'text', 'constraint' => 11, 'null' => true],
            'point' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true, 'default' => null],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('tugas_id', 'jadwal_tugas', 'id', false, 'CASCADE');
        $this->forge->createTable('tugas_siswa', true);

        $fields = [
            'file_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'tugas_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'file' => ['type' => 'text'],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('file_id', true);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('tugas_id', 'jadwal_tugas', 'id', false, 'CASCADE');
        $this->forge->createTable('tugas_siswa_file', true);
    }
    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            try {
                $this->forge->dropForeignKey('jadwal_pelajaran', 'jadwal_pelajaran_kelas_id_foreign');
                $this->forge->dropForeignKey('jadwal_pelajaran', 'jadwal_pelajaran_mapel_id_foreign');
                $this->forge->dropForeignKey('jadwal_pelajaran', 'jadwal_pelajaran_tahun_id_foreign');
                $this->forge->dropForeignKey('jadwal_pelajaran', 'jadwal_pelajaran_user_id_foreign');
                $this->forge->dropForeignKey('jadwal_materi', 'jadwal_materi_jadwal_id_foreign');
                $this->forge->dropForeignKey('jadwal_materi', 'jadwal_materi_kompetensi_id_foreign');
                $this->forge->dropForeignKey('agenda_kegiatan', 'agenda_kegiatan_rpp_id_foreign');
                $this->forge->dropForeignKey('agenda_kegiatan', 'agenda_kegiatan_user_id_foreign');
                $this->forge->dropForeignKey('agenda_kegiatan', 'agenda_kegiatan_jadwal_id_foreign');
                $this->forge->dropForeignKey('jadwal_absensi_siswa', 'jadwal_absensi_siswa_jadwal_id_foreign');
                $this->forge->dropForeignKey('jadwal_absensi_siswa', 'jadwal_absensi_siswa_siswa_id_foreign');
                $this->forge->dropForeignKey('jadwal_tugas', 'jadwal_tugas_jadwal_id_foreign');
                $this->forge->dropForeignKey('jadwal_tugas', 'jadwal_tugas_kompetensi_id_foreign');
                $this->forge->dropForeignKey('jadwal_diskusi', 'jadwal_diskusi_jadwal_id_foreign');
                $this->forge->dropForeignKey('siswa_viewed_materi', 'siswa_viewed_materi_siswa_id_foreign');
                $this->forge->dropForeignKey('siswa_viewed_materi', 'siswa_viewed_materi_materi_id_foreign');
                $this->forge->dropForeignKey('tugas_siswa', 'tugas_siswa_siswa_id_foreign');
                $this->forge->dropForeignKey('tugas_siswa', 'tugas_siswa_tugas_id_foreign');
                $this->forge->dropForeignKey('tugas_siswa_file', 'tugas_siswa_file_siswa_id_foreign');
                $this->forge->dropForeignKey('tugas_siswa_file', 'tugas_siswa_file_tugas_id_foreign');
            } catch (\Throwable $th) {
            }
        }
        $this->forge->dropTable('jadwal_pelajaran', true);
        $this->forge->dropTable('agenda_kegiatan', true);
        $this->forge->dropTable('jadwal_materi', true);
        $this->forge->dropTable('jadwal_tugas', true);
        $this->forge->dropTable('jadwal_absensi_siswa', true);
        $this->forge->dropTable('jadwal_diskusi', true);
        $this->forge->dropTable('siswa_viewed_materi', true);
        $this->forge->dropTable('tugas_siswa', true);
        $this->forge->dropTable('tugas_siswa_file', true);
    }
}
