<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateViolationPointsTables extends Migration
{
    public function up()
    {
        // 1. violation_categories
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => '100'
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true
            ]
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('violation_categories', true);

        // 2. violation_rules
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true
            ],
            'category_id' => [
                'type'     => 'INT',
                'unsigned' => true
            ],
            'code' => [
                'type'       => 'VARCHAR',
                'constraint' => '20'
            ],
            'description' => [
                'type'       => 'VARCHAR',
                'constraint' => '255'
            ],
            'severity' => [
                'type'       => 'ENUM',
                'constraint' => ['Ringan', 'Sedang', 'Berat', 'Khusus'],
                'default'    => 'Ringan'
            ],
            'points' => [
                'type' => 'INT'
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true
            ]
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('category_id', 'violation_categories', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('violation_rules', true);

        // 3. student_violations
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true
            ],
            'student_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true
            ],
            'rule_id' => [
                'type'     => 'INT',
                'unsigned' => true
            ],
            'date' => [
                'type' => 'DATE'
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true
            ],
            'recorded_by' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
                'null'     => true
            ],
            'academic_period_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true
            ]
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('rule_id', 'violation_rules', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('recorded_by', 'users', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('student_violations', true);

        // 4. violation_sanctions
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true
            ],
            'min_points' => [
                'type' => 'INT'
            ],
            'action_title' => [
                'type'       => 'VARCHAR',
                'constraint' => '150'
            ],
            'description' => [
                'type' => 'TEXT'
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true
            ]
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('violation_sanctions', true);

        // ────────────────────────────────────────────────────────────────
        // SEEDING DEFAULT DATA
        // ────────────────────────────────────────────────────────────────
        
        $db = \Config\Database::connect();
        $now = date('Y-m-d H:i:s');

        // Seeding Kategori
        $categories = [
            ['id' => 1, 'name' => 'Kedisiplinan & Ketertiban', 'created_at' => $now, 'updated_at' => $now],
            ['id' => 2, 'name' => 'Kerapian & Estetika',       'created_at' => $now, 'updated_at' => $now],
            ['id' => 3, 'name' => 'Sopan Santun & Etika',      'created_at' => $now, 'updated_at' => $now],
            ['id' => 4, 'name' => 'Akademik & Kejujuran',      'created_at' => $now, 'updated_at' => $now],
            ['id' => 5, 'name' => 'Pelanggaran Berat & Kriminalitas', 'created_at' => $now, 'updated_at' => $now],
        ];
        $db->table('violation_categories')->insertBatch($categories);

        // Seeding Aturan & Poin Pelanggaran
        $rules = [
            // Kategori 1: Kedisiplinan & Ketertiban (id: 1)
            ['category_id' => 1, 'code' => 'DIS-01', 'description' => 'Datang terlambat ke sekolah atau kelas', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 1, 'code' => 'DIS-02', 'description' => 'Keluar kelas tanpa izin guru saat jam pelajaran', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 1, 'code' => 'DIS-03', 'description' => 'Tidak mengikuti upacara bendera hari Senin / hari besar', 'severity' => 'Ringan', 'points' => 10, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 1, 'code' => 'DIS-04', 'description' => 'Bolos (meninggalkan sekolah tanpa izin)', 'severity' => 'Sedang', 'points' => 20, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 1, 'code' => 'DIS-05', 'description' => 'Tidak mengikuti kegiatan ekstrakurikuler wajib', 'severity' => 'Ringan', 'points' => 10, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 1, 'code' => 'DIS-06', 'description' => 'Membawa barang yang tidak berhubungan dengan pelajaran (mainan, dll)', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],

            // Kategori 2: Kerapian & Estetika (id: 2)
            ['category_id' => 2, 'code' => 'RAP-01', 'description' => 'Tidak memakai seragam sesuai ketentuan hari', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-02', 'description' => 'Seragam dikeluarkan/tidak rapi (tidak memakai ikat pinggang/kaos kaki)', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-03', 'description' => 'Siswa laki-laki berambut gondrong/melebihi batas kerah & daun telinga', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-04', 'description' => 'Mewarnai/mengecat rambut dengan warna mencolok', 'severity' => 'Sedang', 'points' => 15, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-05', 'description' => 'Siswa laki-laki bertindik, memakai anting, gelang, atau kalung', 'severity' => 'Sedang', 'points' => 15, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-06', 'description' => 'Siswa perempuan memakai make-up berlebihan / perhiasan mencolok', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 2, 'code' => 'RAP-07', 'description' => 'Memelihara kuku panjang atau memakai cat kuku (kutek)', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],

            // Kategori 3: Sopan Santun & Etika (id: 3)
            ['category_id' => 3, 'code' => 'ETK-01', 'description' => 'Berbicara kasar, kotor, atau tidak sopan kepada teman', 'severity' => 'Ringan', 'points' => 10, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 3, 'code' => 'ETK-02', 'description' => 'Membuat gaduh / mengganggu jalannya kegiatan belajar mengajar', 'severity' => 'Ringan', 'points' => 10, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 3, 'code' => 'ETK-03', 'description' => 'Membuang sampah sembarangan di lingkungan sekolah', 'severity' => 'Ringan', 'points' => 5, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 3, 'code' => 'ETK-04', 'description' => 'Bersikap tidak sopan atau membantah guru / staf sekolah', 'severity' => 'Berat', 'points' => 30, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 3, 'code' => 'ETK-05', 'description' => 'Mengisi absensi secara curang (menitip presensi teman)', 'severity' => 'Sedang', 'points' => 20, 'created_at' => $now, 'updated_at' => $now],

            // Kategori 4: Akademik & Kejujuran (id: 4)
            ['category_id' => 4, 'code' => 'AKD-01', 'description' => 'Menyontek saat ulangan atau ujian berlangsung', 'severity' => 'Sedang', 'points' => 20, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 4, 'code' => 'AKD-02', 'description' => 'Membawa / mengaktifkan HP saat ujian tanpa izin pengawas', 'severity' => 'Sedang', 'points' => 15, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 4, 'code' => 'AKD-03', 'description' => 'Memalsukan tanda tangan orang tua, wali murid, atau guru', 'severity' => 'Sedang', 'points' => 25, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 4, 'code' => 'AKD-04', 'description' => 'Mengubah nilai rapor atau lembar jawaban ujian secara sengaja', 'severity' => 'Berat', 'points' => 50, 'created_at' => $now, 'updated_at' => $now],

            // Kategori 5: Pelanggaran Berat & Kriminalitas (id: 5)
            ['category_id' => 5, 'code' => 'KRM-01', 'description' => 'Merusak sarana prasarana sekolah secara sengaja', 'severity' => 'Berat', 'points' => 50, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-02', 'description' => 'Membawa, menghisap rokok, atau memakai vape di area sekolah', 'severity' => 'Berat', 'points' => 50, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-03', 'description' => 'Melakukan perundungan (bullying) fisik, verbal, atau cyber kepada teman', 'severity' => 'Berat', 'points' => 50, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-04', 'description' => 'Terlibat perkelahian atau tawuran antarpelajar', 'severity' => 'Berat', 'points' => 75, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-05', 'description' => 'Membawa senjata tajam atau barang berbahaya lainnya', 'severity' => 'Khusus', 'points' => 100, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-06', 'description' => 'Membawa, mengonsumsi, atau mengedarkan minuman keras / narkoba', 'severity' => 'Khusus', 'points' => 100, 'created_at' => $now, 'updated_at' => $now],
            ['category_id' => 5, 'code' => 'KRM-07', 'description' => 'Terlibat tindakan asusila, perjudian, pencurian, atau kriminalitas hukum', 'severity' => 'Khusus', 'points' => 100, 'created_at' => $now, 'updated_at' => $now],
        ];
        $db->table('violation_rules')->insertBatch($rules);

        // Seeding Ambang Batas Sanksi
        $sanctions = [
            ['min_points' => 25,  'action_title' => 'Pembinaan Wali Kelas',  'description' => 'Pembinaan oleh Wali Kelas dan Peringatan Lisan.', 'created_at' => $now, 'updated_at' => $now],
            ['min_points' => 50,  'action_title' => 'Peringatan SP 1',        'description' => 'Surat Peringatan 1 (SP 1) & Pemanggilan Orang Tua ke sekolah.', 'created_at' => $now, 'updated_at' => $now],
            ['min_points' => 75,  'action_title' => 'Peringatan SP 2',        'description' => 'Surat Peringatan 2 (SP 2) & Skorsing Akademik selama 3 Hari.', 'created_at' => $now, 'updated_at' => $now],
            ['min_points' => 100, 'action_title' => 'Peringatan SP 3 (Keluar)', 'description' => 'Surat Peringatan 3 (SP 3) & Dikembalikan ke Orang Tua (Dikeluarkan dari sekolah).', 'created_at' => $now, 'updated_at' => $now],
        ];
        $db->table('violation_sanctions')->insertBatch($sanctions);
    }

    public function down()
    {
        $this->forge->dropTable('student_violations', true);
        $this->forge->dropTable('violation_rules', true);
        $this->forge->dropTable('violation_categories', true);
        $this->forge->dropTable('violation_sanctions', true);
    }
}
