<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Finance\ExpenseRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Expense;
use App\Services\Admin\Finance\ExpenseService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class ExpenseController extends Controller
{
    public function __construct(
        private ExpenseService $service
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/finance/expense/index', [
            'expenses' => $this->service->paginated(...$request->validatedWithDefaults()),
        ]);
    }

    public function store(ExpenseRequest $request): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->store($request->validated()),
            'Pengeluaran berhasil ditambahkan.',
            'admin.finance.expenses.index'
        );
    }

    public function update(ExpenseRequest $request, Expense $expense): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->update($expense, $request->validated()),
            'Pengeluaran berhasil diperbarui.',
            'admin.finance.expenses.index'
        );
    }

    public function destroy(Expense $expense): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->destroy($expense),
            'Pengeluaran berhasil dihapus.',
            'admin.finance.expenses.index'
        );
    }
}
