<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Seed awal level disiplin berdasarkan rentang poin dan tindakan sekolah.
 */
class SeedDisciplineLevelsFromPolicy extends Migration
{
    public function up()
    {
        $db = \Config\Database::connect();

        $levels = [
            [
                'min_score'    => 1,
                'max_score'    => 40,
                'title'        => 'Level 1',
                'school_action'=> "Berkomunikasi dengan orang tua/wali siswa.\nTeguran tertulis dan surat perjanjian pertama.",
                'executor'     => 'Wali kelas',
            ],
            [
                'min_score'    => 41,
                'max_score'    => 70,
                'title'        => 'Level 2',
                'school_action'=> "Memberi bimbingan dan perhatian.\nTeguran tertulis dan surat perjanjian kedua.",
                'executor'     => 'Wali kelas dan BK',
            ],
            [
                'min_score'    => 71,
                'max_score'    => 99,
                'title'        => 'Level 3',
                'school_action'=> "Berkomunikasi dengan orang tua/wali siswa.\nMemberi bimbingan dan perhatian.\nSurat perjanjian tertulis bermaterai.\nSkorsing maksimal 3 hari efektif, diketahui Kepala Sekolah.",
                'executor'     => 'Wali kelas, BK, wakasek kesiswaan, diketahui Kepala Sekolah',
            ],
            [
                'min_score'    => 100,
                'max_score'    => null,
                'title'        => 'Level 4',
                'school_action'=> "Berkomunikasi dengan orang tua/wali siswa.\nMemberi bimbingan dan perhatian.\nDikembalikan kepada orang tua secara sepihak.",
                'executor'     => 'Pleno guru',
            ],
        ];

        foreach ($levels as $lvl) {
            // Cegah duplikat berdasarkan min_score & max_score
            $exists = $db->table('discipline_levels')
                ->where('min_score', $lvl['min_score'])
                ->where('max_score', $lvl['max_score'])
                ->countAllResults();

            if ($exists > 0) {
                continue;
            }

            $db->table('discipline_levels')->insert([
                'min_score'    => $lvl['min_score'],
                'max_score'    => $lvl['max_score'],
                'title'        => $lvl['title'],
                'school_action'=> $lvl['school_action'],
                'executor'     => $lvl['executor'],
                'is_active'    => 1,
                'created_at'   => date('Y-m-d H:i:s'),
                'updated_at'   => date('Y-m-d H:i:s'),
            ]);
        }
    }

    public function down()
    {
        // Tidak menghapus data; biarkan tetap ada.
    }
}

