<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ExpenseCategory;
use Illuminate\Http\Request;

class ExpenseCategoryController extends Controller
{
    public function index()
    {
        $categories = ExpenseCategory::orderBy('name')->get();
        return view('admin.expense-categories.index', compact('categories'));
    }

    public function create()
    {
        return view('admin.expense-categories.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        ExpenseCategory::create($validated);

        return redirect()->route('admin.expense-categories.index')
            ->with('success', 'Expense category created successfully');
    }

    public function edit(ExpenseCategory $category)
    {
        return view('admin.expense-categories.edit', compact('category'));
    }

    public function update(Request $request, ExpenseCategory $category)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $category->update($validated);

        return redirect()->route('admin.expense-categories.index')
            ->with('success', 'Expense category updated successfully');
    }

    public function destroy(ExpenseCategory $category)
    {
        $category->delete();
        return redirect()->route('admin.expense-categories.index')
            ->with('success', 'Expense category deleted successfully');
    }
}
