<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RapBonusCair extends Model
{
    protected $table = 'rap_bonus_cair';

    protected $fillable = [
        'rap_id',
        'tahun_ajaran',
        'nominal',
        'rule_bonus_id',
        'total_mahasiswa',
        'validasi_status',
        'catatan_validasi',
        'tanggal_validasi',
        'validasi_by',
        'status',
        'tanggal_pengajuan',
        'pengajuan_by',
        'tanggal_cair',
        'cairkan_by',
        'catatan_histori_bonus',
    ];

    protected $casts = [
        'tanggal_validasi' => 'datetime',
        'tanggal_pengajuan' => 'datetime',
        'tanggal_cair' => 'datetime',
    ];

    public function rap()
    {
        return $this->belongsTo(Rap::class);
    }

    public function ruleBonus()
    {
        return $this->belongsTo(RapBonusRule::class, 'rule_bonus_id');
    }

    public function validator()
    {
        return $this->belongsTo(User::class, 'validasi_by');
    }

    public function pengaju()
    {
        return $this->belongsTo(User::class, 'pengajuan_by');
    }

    public function pencair()
    {
        return $this->belongsTo(User::class, 'cairkan_by');
    }

    // Scopes untuk status validasi
    public function scopeBelumValidasi($query)
    {
        return $query->where('validasi_status', 'belum');
    }

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

    public function scopeInvalid($query)
    {
        return $query->where('validasi_status', 'invalid');
    }

    // Scopes untuk status approval
    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeDisetujui($query)
    {
        return $query->where('status', 'disetujui');
    }

    public function scopeDitolak($query)
    {
        return $query->where('status', 'ditolak');
    }
}
