<?php

namespace Modules\Payroll\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Payroll\DTO\EmployeeLoansFilter;
use Modules\Payroll\Http\Requests\EmployeeLoan\StoreEmployeeLoanRequest;
use Modules\Payroll\Http\Requests\EmployeeLoan\UpdateEmployeeLoanRequest;
use Modules\Payroll\Models\PayrollSetting;
use Modules\Payroll\Services\EmployeeLoanService;
use Modules\Payroll\Transformers\EmployeeLoanResource;

class EmployeeLoanController extends Controller
{
    use AuthorizesRequests;
    public function __construct(protected EmployeeLoanService $employeeLoanService) {}

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('payrollSettingView', PayrollSetting::class);
        $filter = EmployeeLoansFilter::fromRequest($request);
        $response = $this->employeeLoanService->list($filter);

        return (EmployeeLoanResource::collection($response))->additional([
            'status' => true,
            'message' => 'Employee loans retrieved successfully.'
        ]);
    }

    public function store(StoreEmployeeLoanRequest $request)
    {
        $this->authorize('payrollSettingManage', PayrollSetting::class);

        $employeeLoan = $this->employeeLoanService->store($request->validated());

        return (new EmployeeLoanResource($employeeLoan))->additional([
            'status' => true,
            'message' => 'Employee loan created successfully.'
        ]);
    }

    public function show($id)
    {
        $this->authorize('payrollSettingView', PayrollSetting::class);

        return (new EmployeeLoanResource($this->employeeLoanService->show($id)))->additional([
            'status' => true,
            'message' => 'Employee loan retrieved successfully.'
        ]);
    }


    public function update(UpdateEmployeeLoanRequest $request, $employeeLoan)
    {
        $employeeLoan = $this->employeeLoanService->show($employeeLoan);
        $this->authorize('payrollSettingManage', PayrollSetting::class);

        $employeeLoan = $this->employeeLoanService->update($employeeLoan, $request->validated());

        return (new EmployeeLoanResource($employeeLoan))->additional([
            'status' => true,
            'message' => 'Employee loan updated successfully.'
        ]);
    }


    public function approve($employeeLoan)
    {
        $employeeLoan = $this->employeeLoanService->show($employeeLoan);

        $this->authorize('approved', $employeeLoan);

        $employeeLoan = $this->employeeLoanService->approve($employeeLoan);

        return (new EmployeeLoanResource($employeeLoan))->additional([
            'status' => true,
            'message' => 'Employee loan approved successfully.'
        ]);
    }

    public function cancel($employeeLoan)
    {
        $employeeLoan = $this->employeeLoanService->show($employeeLoan);

        $this->authorize('approved', $employeeLoan);

        $employeeLoan = $this->employeeLoanService->cancel($employeeLoan);

        return (new EmployeeLoanResource($employeeLoan))->additional([
            'status' => true,
            'message' => 'Employee loan cancelled successfully.'
        ]);
    }
}
