<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Enums\PayrollAdjustmentType;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Finance\PayrollAdjustmentRequest;
use App\Models\Payroll;
use App\Models\PayrollAdjustment;
use App\Services\Finance\PayrollService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class PayrollController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly PayrollService $payrollService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $periodId = $request->integer('period_id') ?: null;
        $period = $this->payrollService->resolvePeriod($periodId);
        $user = $request->user();

        return Inertia::render('admin/finance/payroll/Index', [
            'periods' => $this->payrollService->listPeriods(),
            'currentPeriod' => $period,
            'payrolls' => $period
                ? $this->payrollService->paginateForPeriod($period, $tableQuery, $user)
                : null,
            'summary' => $period
                ? $this->payrollService->periodSummary($period, $user)
                : null,
            'adjustmentTypes' => PayrollAdjustmentType::selectOptions(),
            'filters' => $this->dataTableFilters($tableQuery, [
                'period_id' => $period?->id,
            ]),
        ]);
    }

    public function storeAdjustment(PayrollAdjustmentRequest $request, Payroll $payroll): RedirectResponse
    {
        $this->payrollService->addAdjustment(
            $payroll,
            $request->validated(),
            $request->user(),
        );

        $this->flashCreated('Penyesuaian gaji');

        return redirect()->route('admin.finance.payroll.index', [
            'period_id' => $payroll->payroll_period_id,
        ]);
    }

    public function updateAdjustment(PayrollAdjustmentRequest $request, PayrollAdjustment $payrollAdjustment): RedirectResponse
    {
        $this->payrollService->updateAdjustment(
            $payrollAdjustment,
            $request->validated(),
            $request->user(),
        );

        $this->flashUpdated('Penyesuaian gaji');

        return redirect()->route('admin.finance.payroll.index', [
            'period_id' => $payrollAdjustment->payroll->payroll_period_id,
        ]);
    }

    public function destroyAdjustment(PayrollAdjustment $payrollAdjustment): RedirectResponse
    {
        $payroll = $payrollAdjustment->payroll;

        $this->payrollService->deleteAdjustment($payrollAdjustment);

        $this->flashDeleted('Penyesuaian gaji');

        return redirect()->route('admin.finance.payroll.index', [
            'period_id' => $payroll->payroll_period_id,
        ]);
    }
}
