<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class StandarKompetensiJabatanSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $kompetensiList = \App\Models\Kompetensi::all();
        $jenjangs = [
            1 => 2, // Ahli Pertama -> Level 2
            2 => 3, // Ahli Muda -> Level 3
            3 => 4, // Ahli Madya -> Level 4
            4 => 4, // Ahli Utama -> Level 4 (with exception for integritas)
        ];

        $data = [];

        foreach ($jenjangs as $jenjangId => $baseLevel) {
            foreach ($kompetensiList as $kompetensi) {
                $level = $baseLevel;

                // Rule: Ahli Utama (id 4), Integritas = Level 5
                if ($jenjangId === 4 && stripos($kompetensi->nama_kompetensi, 'integritas') !== false) {
                    $level = 5;
                }

                $data[] = [
                    'jenjang_jabatan_id' => $jenjangId,
                    'kompetensi_id' => $kompetensi->id,
                    'level' => $level,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }
        }

        // Clear existing data to avoid unique constraint violations
        \DB::table('standar_kompetensi_jabatan')->truncate();

        // Insert batch
        \DB::table('standar_kompetensi_jabatan')->insert($data);
    }
}
