<?php

namespace App\Services;

use App\Models\RombelModel;

class RombelSyncService
{
    public static function syncTingkat($kelas, $tingkatBaru)
    {
        $rombelModel = new RombelModel();

        $rombels = $rombelModel
            ->where('kelas_id', $kelas['id'])
            ->findAll();

        foreach ($rombels as $rombel) {

            // contoh: VIII-A
            $huruf = explode('-', $rombel['nama_rombel'])[1] ?? $rombel['urutan'];

            $namaBaru = self::romawi($tingkatBaru) . '-' . $huruf;

            $rombelModel->update($rombel['id'], [
                'tingkat' => $tingkatBaru,
                'nama_rombel' => $namaBaru
            ]);
        }
    }

    private static function romawi($angka)
    {
        $map = [
            1 => 'I', 2 => 'II', 3 => 'III',
            4 => 'IV', 5 => 'V', 6 => 'VI',
            7 => 'VII', 8 => 'VIII', 9 => 'IX',
            10 => 'X', 11 => 'XI', 12 => 'XII'
        ];

        return $map[$angka] ?? $angka;
    }
}
