<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\PengajuanDetail;
use App\Models\Kriteria;
use App\Models\SubKriteria;

class FixPengajuanDetailsRelations extends Migration
{
    public function up()
    {
        $kriteriaData = [
            'Penghasilan Orang Tua' => [
                '< 2 juta' => 4,
                '2 - 3 juta' => 3,
                '3 - 4 juta' => 2,
                '> 5 juta' => 1
            ],

            'Pekerjaan Orang Tua' => [
                'Tidak Bekerja/Penerima Pendapatan' => 3,
                'Bekerja Tidak Tetap' => 2,
                'Bekerja Tetap' => 1
            ],
                
            'Tanggungan Orang Tua' => [
                '> 4 Orang' => 3,
                '2 - 4 Orang' => 2,
                '1 Orang' => 1
            ],
                
            'Status Orang Tua' => [
                'Yatim Piatu' => 3,
                'Cerai/Yatim/Piatu' => 2,
                'Lengkap' => 1
            ],
                    
            'Kondisi Rumah' => [
                'Menumpang' => 3,
                'Kontrak' => 2,
                'Milik Sendiri' => 1
            ],
                    
            'Kondisi Kendaraan' => [
                'Tidak Punya' => 3,
                'Motor' => 2,
                'Mobil' => 1
            ],
                    
            'Semester' => [
                '1' => 6,    
                '2' => 5,
                '3' => 4,
                '4' => 3,
                '5' => 2,
                '>6' => 1     
            ],
                    
            'IPK' => [
                '< 2.75' => 1,
                '2.75 - 3.00' => 2,
                '3.00 - 3.50' => 3,
                '> 3.50' => 4
            ],
        ];

        foreach ($kriteriaData as $kriteriaName => $subkriteriaValues) {
            $kriteria = Kriteria::where('nama_kriteria', $kriteriaName)->first();
            
            if ($kriteria) {
                // Update kriteria_id
                PengajuanDetail::where('kriteria', $kriteriaName)
                    ->whereNull('kriteria_id')
                    ->update(['kriteria_id' => $kriteria->id]);
    
                // Update sub_kriteria_id
                foreach ($subkriteriaValues as $subName => $nilai) {
                    $subKriteria = SubKriteria::where('nama_subkriteria', $subName) // Note: using nama_subkriteria
                                       ->where('kriteria_id', $kriteria->id)
                                       ->first();
                    
                    if ($subKriteria) {
                        PengajuanDetail::where('kriteria', $kriteriaName)
                            ->where('subkriteria_text', $subName)
                            ->whereNull('sub_kriteria_id')
                            ->update(['sub_kriteria_id' => $subKriteria->id]);
                    }
                }
            }
        }
    }

    public function down()
    {

    }
}