<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Alumni extends Model
{
    protected $fillable = [
        'id_alumni', 
        'nama_alumni',
        'perguruan_tinggi',
        'jurusan_diambil',
        'profil_nilai',
        'profil_minat'
    ];

    protected static function boot()
    {
        parent::boot();

        // Otomatis membuat id_alumni yang unik dengan aman saat data dibuat
        static::creating(function ($model) {
            if (empty($model->id_alumni)) {
                $nextNumber = ((int) static::max('id')) + 1;
                $candidate = 'ALM' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);

                while (static::where('id_alumni', $candidate)->exists()) {
                    $nextNumber++;
                    $candidate = 'ALM' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
                }
                
                $model->id_alumni = $candidate;
            }
        });
    }
}