<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Http\Controllers\Controller;
use App\Http\Requests\Groups\UpdateGroupRecruitmentRequest;
use App\Models\Group;
use App\Services\GroupRecruitmentService;
use App\Services\GroupService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class GroupRecruitmentStudioController extends Controller
{
    public function __construct(
        private readonly GroupService $groups,
        private readonly GroupRecruitmentService $recruitment,
    ) {
    }

    public function show(Request $request, Group $group): Response
    {
        $this->authorize('manageRecruitment', $group);

        return Inertia::render('Studio/StudioGroupRecruitment', [
            'title' => $group->name . ' Recruitment',
            'description' => 'Show open roles publicly, describe your workflow, and control how applicants should get in touch.',
            'studioGroup' => $this->groups->mapGroupDetail($group, $request->user()),
            'recruitment' => $this->groups->recruitmentPayload($group),
            'contactModes' => [
                ['value' => 'join_request', 'label' => 'Join request'],
                ['value' => 'direct_message', 'label' => 'Direct message'],
                ['value' => 'external_link', 'label' => 'External link'],
            ],
            'visibilityOptions' => [
                ['value' => 'public', 'label' => 'Public'],
                ['value' => 'members_only', 'label' => 'Members only'],
                ['value' => 'private', 'label' => 'Private'],
            ],
            'roleOptions' => collect(config('groups.recruitment.roles', []))
                ->map(fn (string $role): array => ['value' => $role, 'label' => $role])
                ->values()
                ->all(),
            'skillOptions' => collect(config('groups.recruitment.skills', []))
                ->map(fn (string $skill): array => ['value' => $skill, 'label' => $skill])
                ->values()
                ->all(),
            'updateUrl' => route('studio.groups.recruitment.update', ['group' => $group]),
        ]);
    }

    public function update(UpdateGroupRecruitmentRequest $request, Group $group): RedirectResponse
    {
        $this->authorize('manageRecruitment', $group);

        $this->recruitment->upsert($group, $request->validated(), $request->user());

        return back()->with('success', 'Recruitment profile updated.');
    }
}