<?php

namespace App\Http\Controllers\Api\V1\Recruitment;

use App\Domain\Recruitment\Enums\EmployerStatus;
use App\Domain\Recruitment\Models\Employer;
use App\Domain\Recruitment\Services\EmployerService;
use App\Http\Controllers\Controller;
use App\Http\Requests\Recruitment\ChangeEmployerStatusRequest;
use App\Http\Resources\EmployerResource;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;

class EmployerStatusController extends Controller
{
    public function __construct(private readonly EmployerService $employers) {}

    public function __invoke(ChangeEmployerStatusRequest $request, Employer $employer): JsonResponse
    {
        $target = $request->status();

        // Activating or blacklisting an employer is an approval decision.
        $ability = in_array($target, [EmployerStatus::Active, EmployerStatus::Blacklisted], true)
            ? 'approve'
            : 'update';

        $this->authorize($ability, $employer);

        $employer = $this->employers->changeStatus($employer, $target, $request->input('reason'), $request->user());

        return ApiResponse::success(
            new EmployerResource($employer->load(['branch:id,uuid,name', 'contacts'])),
            "Employer marked {$target->label()}.",
        );
    }
}
