<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PengajuanKeuangan extends Model
{
    protected $table = 'pengajuan_keuangans';

    protected $fillable = [
        'instansi_id',
        'user_id',
        'judul',
        'kategori',
        'nominal',
        'deskripsi',
        'file_pendukung',
        'status',
        'alasan_tolak',
        'approved_by_admin',
    ];

    public function getStatusLabelAttribute()
    {
        $labels = [
            'pending'   => 'Menunggu Admin',
            'diproses'  => 'Sedang Diproses',
            'selesai'   => 'Selesai',
            'ditolak'   => 'Ditolak',
        ];
        return $labels[$this->status] ?? $this->status;
    }

    public function getStatusColorAttribute()
    {
        $colors = [
            'pending'  => ['bg' => '#fff7ed', 'text' => '#c2410c'],
            'diproses' => ['bg' => '#eff6ff', 'text' => '#1d4ed8'],
            'selesai'  => ['bg' => '#f0fdf4', 'text' => '#15803d'],
            'ditolak'  => ['bg' => '#fef2f2', 'text' => '#dc2626'],
        ];
        return $colors[$this->status] ?? $colors['pending'];
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function instansi()
    {
        return $this->belongsTo(Instansi::class);
    }

    public function histories()
    {
        return $this->hasMany(PengajuanHistory::class, 'pengajuan_id');
    }

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