<?php

namespace App\Models;

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

class OfficeExpense extends Model
{
    use HasFactory;
    
    protected $guarded = [];
    
    protected $casts = [
        'expense_date' => 'date',
        'amount' => 'float',
    ];
    
    /**
     * Get monthly total expenses
     */
    public static function getMonthlyTotal($year, $month)
    {
        return static::whereYear('expense_date', $year)
                    ->whereMonth('expense_date', $month)
                    ->sum('amount');
    }
    
    /**
     * Get overall total expenses
     */
    public static function getTotalExpenses()
    {
        return static::sum('amount');
    }
    
    /**
     * Get recent expenses
     */
    public static function getRecentExpenses($limit = 10)
    {
        return static::orderBy('expense_date', 'DESC')
                    ->limit($limit)
                    ->get();
    }
    
    /**
     * Get expenses by category
     */
    public static function getCategoryTotal($category, $year = null, $month = null)
    {
        $query = static::where('expense_category', $category);
        
        if ($year && $month) {
            $query->whereYear('expense_date', $year)
                  ->whereMonth('expense_date', $month);
        }
        
        return $query->sum('amount');
    }
}