<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Finance\ExpenseRequest;
use App\Models\Expense;
use App\Services\Finance\ExpenseService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class ExpenseController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly ExpenseService $expenseService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);

        return Inertia::render('admin/finance/expenses/Index', [
            'expenses' => $this->expenseService->paginateForIndex($tableQuery),
            'filters' => $this->dataTableFilters($tableQuery),
        ]);
    }

    public function store(ExpenseRequest $request): RedirectResponse
    {
        $this->expenseService->create($request->validated(), $request->user());

        $this->flashCreated('Pengeluaran');

        return redirect()->route('admin.finance.expenses.index');
    }

    public function update(ExpenseRequest $request, Expense $expense): RedirectResponse
    {
        $this->expenseService->update($expense, $request->validated());

        $this->flashUpdated('Pengeluaran');

        return redirect()->route('admin.finance.expenses.index');
    }

    public function destroy(Expense $expense): RedirectResponse
    {
        $this->expenseService->delete($expense);

        $this->flashDeleted('Pengeluaran');

        return redirect()->route('admin.finance.expenses.index');
    }
}
