<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Http\Requests\Member\StoreCvProfileRequest;
use App\Services\Member\CvService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

// ══════════════════════════════════════════════════════════════════
//  InPractice — Member\CvController
//  Location: app/Http/Controllers/Member/CvController.php
//
//  Thin controller — zero business logic.
//  All logic lives in CvService.
//
//  Routes:
//    member.cv.pool        GET    /app/job-club/cv-pool
//    member.cv.profile     GET    /app/job-club/my-cv
//    member.cv.save        POST   /app/job-club/my-cv
//    member.cv.toggle      PATCH  /app/job-club/my-cv/toggle-visibility
// ══════════════════════════════════════════════════════════════════

class CvController extends Controller
{
    public function __construct(private CvService $cvService) {}

    // Browse the anonymous CV pool
    public function pool(Request $request): Response
    {
        $data = $this->cvService->index($request->user(), $request->only([
            'hub_id', 'experience_level', 'job_type_preference', 'years_experience_min',
        ]));

        return Inertia::render('Member/JobClub/CvPool/Index', $data);
    }

    // My CV profile — view and edit
    public function myProfile(Request $request): Response
    {
        $data = $this->cvService->myProfile($request->user());

        return Inertia::render('Member/JobClub/CvPool/MyProfile', $data);
    }

    // Create or update CV profile
    public function save(StoreCvProfileRequest $request): \Illuminate\Http\RedirectResponse
    {
        $this->cvService->saveProfile($request->user(), $request->validated());

        return back()->with('success', __('cv.profile_saved'));
    }

    // Toggle visibility in the pool
    public function toggleVisibility(Request $request): \Illuminate\Http\JsonResponse
    {
        $newState = $this->cvService->toggleVisibility($request->user());

        return response()->json([
            'is_visible' => $newState,
            'message'    => $newState ? __('cv.now_visible') : __('cv.now_hidden'),
        ]);
    }
}
