<?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\DestroyEmployeeAdvanceRequest;
use App\Http\Requests\Admin\Finance\EmployeeAdvanceRequest;
use App\Http\Requests\Admin\Finance\PayEmployeeAdvanceRequest;
use App\Http\Requests\Admin\Finance\RejectEmployeeAdvanceRequest;
use App\Models\EmployeeAdvance;
use App\Services\Finance\EmployeeAdvanceService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class EmployeeAdvanceController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly EmployeeAdvanceService $employeeAdvanceService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $status = $request->string('status')->toString();
        $pageData = $this->employeeAdvanceService->indexPageData($tableQuery, $request->user(), $status);

        return Inertia::render('admin/finance/employee-advances/Index', [
            ...$pageData,
            'filters' => $this->dataTableFilters($tableQuery, [
                'status' => $status,
            ]),
        ]);
    }

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

        $this->flashSuccess('Kasbon berhasil diajukan.');

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

    public function update(EmployeeAdvanceRequest $request, EmployeeAdvance $employeeAdvance): RedirectResponse
    {
        $this->employeeAdvanceService->update($employeeAdvance, $request->validated(), $request->user());

        $this->flashUpdated('Kasbon');

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

    public function destroy(DestroyEmployeeAdvanceRequest $request, EmployeeAdvance $employeeAdvance): RedirectResponse
    {
        $this->employeeAdvanceService->delete($employeeAdvance, $request->user());

        $this->flashDeleted('Kasbon');

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

    public function approve(Request $request, EmployeeAdvance $employeeAdvance): RedirectResponse
    {
        $this->employeeAdvanceService->approve($employeeAdvance, $request->user());

        $this->flashSuccess('Kasbon berhasil disetujui dan dicairkan dari kas.');

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

    public function reject(RejectEmployeeAdvanceRequest $request, EmployeeAdvance $employeeAdvance): RedirectResponse
    {
        $this->employeeAdvanceService->reject(
            $employeeAdvance,
            $request->validated('reason'),
            $request->user(),
        );

        $this->flashSuccess('Kasbon berhasil ditolak.');

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

    public function pay(PayEmployeeAdvanceRequest $request, EmployeeAdvance $employeeAdvance): RedirectResponse
    {
        $payAmount = $request->input('amount') ? (int) $request->input('amount') : null;

        $this->employeeAdvanceService->pay($employeeAdvance, $request->user(), $payAmount);

        $this->flashSuccess('Kasbon berhasil dibayar.');

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