<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class PengajuanUkt extends Model
{
    use HasFactory;

    protected $table = 'pengajuan_ukts';
    protected $primaryKey = 'id';

    protected $attributes = [
        'status_validasi' => 'menunggu'
    ];

    protected $casts = [
        'status_validasi' => 'string',
    ];

    protected $fillable = [
        'mahasiswa_id',
        'jenis_pengajuan',
        'status_validasi',
        'alasan_penolakan',
        'status_form_id',
        'ukt_saat_ini',
        'file_ukt',
        'file_ukt_valid',
        'file_ukt_alasan',
        'alasan_pengajuan' 
    ];

    protected $appends = ['status', 'semester'];

    // Event untuk handle penghapusan
    protected static function booted()
    {
        static::deleting(function ($pengajuan) {
            // Hapus semua relasi terlebih dahulu
            $pengajuan->hasilPenilaian()->delete();
            $pengajuan->details()->delete();
            $pengajuan->dokumen()->delete();
            
            // Hapus file-file terkait jika ada
            foreach ($pengajuan->details as $detail) {
                if ($detail->file_dokumen && Storage::exists('public/'.$detail->file_dokumen)) {
                    Storage::delete('public/'.$detail->file_dokumen);
                }
            }

            // Hapus notifikasi terkait pengajuan ini
            if ($pengajuan->mahasiswa && $pengajuan->mahasiswa->user) {
                $pengajuan->mahasiswa->user->notifications()
                    ->where('data->pengajuan_id', $pengajuan->id)
                    ->delete();
            }
        });
    }

    public function getStatusAttribute()
    {
        return $this->attributes['status_validasi'] ?? 'menunggu';
    }

    public function setStatusAttribute($value)
    {
        $this->attributes['status_validasi'] = $value;
    }

    public function mahasiswa()
    {
        return $this->belongsTo(Mahasiswa::class, 'mahasiswa_id', 'user_id');
    }

    public function hasilPenilaian()
{
    return $this->hasOne(HasilPenilaian::class, 'pengajuan_id');
}

    public function details()
    {
        return $this->hasMany(PengajuanDetail::class, 'pengajuan_ukt_id');
    }

    public function validator()
    {
        return $this->belongsTo(User::class, 'validator_id');
    }
    
    public function dokumen()
    {
        return $this->hasMany(PengajuanDetail::class, 'pengajuan_ukt_id');
    }

    public function form()
{
    return $this->belongsTo(Form::class, 'status_form_id');
}

    public function isDibuka()
    {
        return $this->form->status === 'Dibuka';
    }

    public function isDitutup()
    {
        return optional($this->form)->status === 'Ditutup';
    }

    public function isPenurunan()
    {
        return $this->jenis_pengajuan === 'penurunan';
    }

    public function isPengangsuran()
    {
        return $this->jenis_pengajuan === 'pengangsuran';
    }

    public function isMenunggu()
    {
        return $this->getStatusAttribute() === 'menunggu';
    }

    public function isValid()
    {
        return $this->getStatusAttribute() === 'valid';
    }

    public function isTidakValid()
    {
        return $this->getStatusAttribute() === 'tidak valid';
    }


    public function scopeMenunggu($query)
{
    return $query->where('status_validasi', 'menunggu');
}

    public function scopeValid($query)
{
    return $query->where('status_validasi', 'valid');
}

    public function scopeTidakValid($query)
{
    return $query->where('status_validasi', 'tidak valid');
}

    public function scopePenurunan($query)
    {
        return $query->where('jenis_pengajuan', 'penurunan');
    }

    public function scopePengangsuran($query)
    {
        return $query->where('jenis_pengajuan', 'pengangsuran');
    }

    public function getSemesterAttribute()
    {
        return optional($this->form)->semester;
    }

    public function setUktSaatIniAttribute($value)
    {
        $this->attributes['ukt_saat_ini'] = (int) str_replace('.', '', $value);
    }

    public function getUktFormattedAttribute()
    {
        return number_format($this->ukt_saat_ini, 0, ',', '.');
    }

}
