<?php

namespace App\Http\Controllers;

use App\Models\Expense;
use Illuminate\Http\Request;

class ExpenseController extends Controller
{
    public function index()
    {
        $expenses = Expense::orderBy('expense_date', 'desc')->paginate(15);
        $totalExpenses = Expense::sum('amount');
        return view('admin.finance.expenses', compact('expenses', 'totalExpenses'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'category' => 'required|string',
            'description' => 'required|string',
            'amount' => 'required|numeric|min:0',
            'vendor' => 'nullable|string',
            'expense_date' => 'required|date',
            'payment_method' => 'required|string',
        ]);

        Expense::create($validated);

        return redirect()->route('admin.finance.expenses')->with('success', 'Operating expense recorded successfully!');
    }
}
