<?php

namespace App\Modules\HR\Recruitment\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Recruitment\Models\RecruitmentVacancy;
use App\Modules\HR\Recruitment\Repositories\CareerPortalVacancyRepository;
use App\Modules\HR\Recruitment\Requests\CareerPortalApplyRequest;
use App\Modules\HR\Recruitment\Services\CareerPortalApplyService;
use App\Modules\HR\Recruitment\Services\CareerPortalTrackingService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class CareerPortalPublicController extends Controller
{
    public function __construct(
        private readonly CareerPortalVacancyRepository $vacancies,
        private readonly CareerPortalApplyService $applyService,
        private readonly CareerPortalTrackingService $tracking,
    ) {}

    public function home(Request $request): View
    {
        $tenant = $request->attributes->get('career_tenant');
        $settings = $request->attributes->get('career_settings');

        return view('pages.careers.home', [
            'tenant' => $tenant,
            'settings' => $settings,
            'featured' => $this->vacancies->featured((int) $tenant->id),
            'jobs' => $this->vacancies->paginatePublished((int) $tenant->id, [], 6),
        ]);
    }

    public function jobs(Request $request): View
    {
        $tenant = $request->attributes->get('career_tenant');

        return view('pages.careers.jobs', [
            'tenant' => $tenant,
            'settings' => $request->attributes->get('career_settings'),
            'jobs' => $this->vacancies->paginatePublished((int) $tenant->id, [
                'q' => $request->string('q')->toString() ?: null,
                'featured' => $request->boolean('featured'),
            ]),
        ]);
    }

    public function show(Request $request, string $tenant, string $vacancy): View
    {
        $tenantModel = $request->attributes->get('career_tenant');
        $job = $this->vacancies->findPublishedBySlug((int) $tenantModel->id, $vacancy);
        abort_if($job === null, 404);

        return view('pages.careers.show', [
            'tenant' => $tenantModel,
            'settings' => $request->attributes->get('career_settings'),
            'job' => $job,
        ]);
    }

    public function applyForm(Request $request, string $tenant, string $vacancy): View
    {
        $tenantModel = $request->attributes->get('career_tenant');
        $job = $this->vacancies->findPublishedBySlug((int) $tenantModel->id, $vacancy);
        abort_if($job === null, 404);

        return view('pages.careers.apply', [
            'tenant' => $tenantModel,
            'settings' => $request->attributes->get('career_settings'),
            'job' => $job,
            'recaptchaSiteKey' => config('services.recaptcha.site_key'),
        ]);
    }

    public function applyStore(
        CareerPortalApplyRequest $request,
        string $tenant,
        string $vacancy,
    ): RedirectResponse {
        $tenantModel = $request->attributes->get('career_tenant');
        $job = $this->vacancies->findPublishedBySlug((int) $tenantModel->id, $vacancy);
        abort_if($job === null, 404);

        $application = $this->applyService->apply(
            $job,
            $request->validated(),
            $request->file('cv'),
        );

        return redirect()
            ->route('careers.confirmation', [
                'tenant' => $tenantModel->slug,
                'token' => $application->tracking_token,
            ]);
    }

    public function confirmation(Request $request, string $tenant, string $token): View
    {
        $tenantModel = $request->attributes->get('career_tenant');
        $application = $this->tracking->findByToken((int) $tenantModel->id, $token);

        return view('pages.careers.confirmation', [
            'tenant' => $tenantModel,
            'settings' => $request->attributes->get('career_settings'),
            'application' => $application,
        ]);
    }

    public function trackForm(Request $request): View
    {
        return view('pages.careers.track-form', [
            'tenant' => $request->attributes->get('career_tenant'),
            'settings' => $request->attributes->get('career_settings'),
        ]);
    }

    public function trackLookup(Request $request): RedirectResponse
    {
        $tenant = $request->attributes->get('career_tenant');
        $token = trim((string) $request->input('token'));
        $this->tracking->findByToken((int) $tenant->id, $token);

        return redirect()->route('careers.track.show', [
            'tenant' => $tenant->slug,
            'token' => $token,
        ]);
    }

    public function trackShow(Request $request, string $tenant, string $token): View
    {
        $tenantModel = $request->attributes->get('career_tenant');
        $application = $this->tracking->findByToken((int) $tenantModel->id, $token);

        return view('pages.careers.track', [
            'tenant' => $tenantModel,
            'settings' => $request->attributes->get('career_settings'),
            'application' => $application,
            'offers' => $this->tracking->visibleOffers($application),
        ]);
    }
}
