<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CashModal extends Model
{
    use HasFactory;

    protected $fillable = [
        'amount',
        'modal_date',
        'type',
        'account',
        'notes',
        'user_id',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'modal_date' => 'date',
    ];

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

    public function scopePetty($query)
    {
        return $query->where('account', 'petty');
    }

    public function scopeCash($query)
    {
        return $query->where('account', 'cash');
    }

    public function scopeBank($query)
    {
        return $query->where('account', 'bank');
    }

    public function scopeForDate($query, $date)
    {
        return $query->whereDate('modal_date', $date);
    }

    public function scopeForMonth($query, $year, $month)
    {
        return $query->whereYear('modal_date', $year)
            ->whereMonth('modal_date', $month);
    }

    public function getTypeLabel(): string
    {
        return match ($this->type) {
            'initial' => 'Modal Awal',
            'topup' => 'Tambah Modal',
            default => $this->type,
        };
    }

    public function getAccountLabel(): string
    {
        return match ($this->account) {
            'petty' => 'Kas Kecil',
            'cash' => 'Kas Tunai',
            'bank' => 'Kas Bank',
            default => $this->account,
        };
    }
}