<?php

namespace App\Modules\HR\EmployeeSelfService\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\EmployeeSelfService\Requests\MeLoanRequest;
use App\Modules\HR\EmployeeSelfService\Services\SelfEmployeeContextService;
use App\Modules\HR\Loans\DTOs\LoanData;
use App\Modules\HR\Loans\Repositories\LoanRepository;
use App\Modules\HR\Loans\Services\LoanService;
use Illuminate\Http\JsonResponse;

class MeLoanController extends Controller
{
    public function __construct(
        private readonly SelfEmployeeContextService $context,
        private readonly LoanRepository $repository,
        private readonly LoanService $service,
    ) {}

    public function index(): JsonResponse
    {
        $employee = $this->context->employee();

        return response()->json($this->repository->paginate((int) $employee->tenant_id, (int) $employee->id));
    }

    public function create(): JsonResponse
    {
        return response()->json(['types' => ['loan', 'advance']]);
    }

    public function store(MeLoanRequest $request): JsonResponse
    {
        $employee = $this->context->employee();
        $data = $request->validated();
        $data['employee_id'] = $employee->id;

        return response()->json($this->service->create(LoanData::fromValidated($data), (int) $employee->tenant_id, (int) $request->user()->id), 201);
    }
}
