<?php

namespace App\Modules\HR\Recruitment\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Recruitment\DTOs\CandidateWriteData;
use App\Modules\HR\Recruitment\Models\RecruitmentCandidate;
use App\Modules\HR\Recruitment\Repositories\RecruitmentCandidateCvRepository;
use App\Modules\HR\Recruitment\Repositories\RecruitmentCandidateRepository;
use App\Modules\HR\Recruitment\Requests\StoreCandidateRequest;
use App\Modules\HR\Recruitment\Requests\UpdateCandidateRequest;
use App\Modules\HR\Recruitment\Services\RecruitmentCandidateService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class CandidatesPageController extends Controller
{
    public function __construct(
        private readonly RecruitmentCandidateService $service,
        private readonly RecruitmentCandidateRepository $repository,
        private readonly RecruitmentCandidateCvRepository $cvs,
    ) {}

    public function index(Request $request): View
    {
        $this->authorize('viewAny', RecruitmentCandidate::class);

        return view('pages.hr.recruitment.candidates.index', [
            'candidates' => $this->repository->paginateForIndex($this->tenantId(), $request),
        ]);
    }

    public function create(): View
    {
        $this->authorize('create', RecruitmentCandidate::class);

        return view('pages.hr.recruitment.candidates.create');
    }

    public function store(StoreCandidateRequest $request): RedirectResponse
    {
        $this->service->store(
            CandidateWriteData::fromValidated($request->validated()),
            (int) $request->user()->id,
        );

        return redirect()
            ->route('hr.recruitment.candidates.index')
            ->with('status', __('recruitment.candidates.flash.created'));
    }

    public function edit(RecruitmentCandidate $candidate): View
    {
        $this->authorize('update', $candidate);

        return view('pages.hr.recruitment.candidates.edit', [
            'candidate' => $candidate,
            'cvs' => $this->cvs->listForCandidate((int) $candidate->id),
        ]);
    }

    public function update(UpdateCandidateRequest $request, RecruitmentCandidate $candidate): RedirectResponse
    {
        $this->service->update(
            $candidate,
            CandidateWriteData::fromValidated($request->validated()),
            (int) $request->user()->id,
        );

        return redirect()
            ->route('hr.recruitment.candidates.index')
            ->with('status', __('recruitment.candidates.flash.updated'));
    }

    public function destroy(RecruitmentCandidate $candidate): RedirectResponse
    {
        $this->authorize('delete', $candidate);
        $this->service->destroy($candidate);

        return redirect()
            ->route('hr.recruitment.candidates.index')
            ->with('status', __('recruitment.candidates.flash.deleted'));
    }

    private function tenantId(): int
    {
        $id = auth()->user()?->tenant_id;
        abort_if($id === null, 403);

        return (int) $id;
    }
}
