<?php

namespace App\Modules\HR\Recruitment\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\HR\Recruitment\Models\RecruitmentVacancy;
use App\Modules\HR\Recruitment\Requests\UpdateCareerPortalSettingsRequest;
use App\Modules\HR\Recruitment\Requests\UpdateVacancyPortalMetaRequest;
use App\Modules\HR\Recruitment\Services\CareerPortalSettingsService;
use App\Modules\HR\Recruitment\Services\CareerPortalVacancyService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class CareerPortalAdminController extends Controller
{
    public function __construct(
        private readonly CareerPortalSettingsService $settings,
        private readonly CareerPortalVacancyService $portalVacancies,
    ) {}

    public function show(): View
    {
        abort_unless(auth()->user()?->can('recruitment.view'), 403);
        $tenant = Tenant::query()->whereKey(auth()->user()->tenant_id)->firstOrFail();
        $settings = $this->settings->getOrDefaults();

        return view('pages.hr.recruitment.career-portal.index', [
            'settings' => $settings,
            'tenant' => $tenant,
            'publicUrl' => route('careers.home', ['tenant' => $tenant->slug]),
        ]);
    }

    public function updateSettings(UpdateCareerPortalSettingsRequest $request): RedirectResponse
    {
        $this->settings->save($request->validated());

        return back()->with('status', __('recruitment.career_portal.flash.settings_saved'));
    }

    public function publish(RecruitmentVacancy $vacancy): RedirectResponse
    {
        $this->authorize('update', $vacancy);
        $this->portalVacancies->publishToPortal($vacancy);

        return back()->with('status', __('recruitment.career_portal.flash.published'));
    }

    public function unpublish(RecruitmentVacancy $vacancy): RedirectResponse
    {
        $this->authorize('update', $vacancy);
        $this->portalVacancies->unpublishFromPortal($vacancy);

        return back()->with('status', __('recruitment.career_portal.flash.unpublished'));
    }

    public function feature(RecruitmentVacancy $vacancy): RedirectResponse
    {
        $this->authorize('update', $vacancy);
        $this->portalVacancies->toggleFeatured($vacancy, true);

        return back()->with('status', __('recruitment.career_portal.flash.featured'));
    }

    public function unfeature(RecruitmentVacancy $vacancy): RedirectResponse
    {
        $this->authorize('update', $vacancy);
        $this->portalVacancies->toggleFeatured($vacancy, false);

        return back()->with('status', __('recruitment.career_portal.flash.unfeatured'));
    }

    public function updateMeta(
        UpdateVacancyPortalMetaRequest $request,
        RecruitmentVacancy $vacancy,
    ): RedirectResponse {
        $this->authorize('update', $vacancy);
        $this->portalVacancies->updatePortalMeta($vacancy, $request->validated());

        return back()->with('status', __('recruitment.career_portal.flash.meta_saved'));
    }
}
