<?php

namespace App\Models;

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

class CashMutation extends Model
{
    use HasFactory;

    protected $fillable = [
        'amount',
        'mutation_date',
        'from_account',
        'to_account',
        'notes',
        'user_id',
    ];

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

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

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

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

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

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

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

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

    public function getDescription(): string
    {
        return $this->getFromAccountLabel() . ' → ' . $this->getToAccountLabel();
    }
}