<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Http\Requests\Member\RespondJobInterestRequest;
use App\Http\Requests\Member\SendJobInterestRequest;
use App\Http\Requests\Member\StoreJobListingRequest;
use App\Models\CvProfile;
use App\Models\JobListing;
use App\Models\JobNotification;
use App\Services\Member\JobService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

// ══════════════════════════════════════════════════════════════════
//  InPractice — Member\JobController
//  Location: app/Http/Controllers/Member/JobController.php
//
//  Thin controller — zero business logic.
//  All logic lives in JobService.
//
//  Routes:
//    member.jobs.index         GET    /app/job-club/listings
//    member.jobs.create        GET    /app/job-club/listings/create
//    member.jobs.store         POST   /app/job-club/listings
//    member.jobs.show          GET    /app/job-club/listings/{listing}
//    member.jobs.close         PATCH  /app/job-club/listings/{listing}/close
//    member.jobs.reopen        PATCH  /app/job-club/listings/{listing}/reopen
//    member.jobs.apply         POST   /app/job-club/listings/{listing}/apply
//    member.jobs.send-direct   POST   /app/job-club/cv-pool/{cv}/send/{listing}
//    member.jobs.respond       PATCH  /app/job-club/notifications/{notification}/respond
//    member.jobs.consent       PATCH  /app/job-club/notifications/{notification}/consent
//    member.jobs.notifications GET    /app/job-club/my-notifications
//    member.jobs.initiate      PATCH  /app/job-club/notifications/{notification}/initiate
//    member.jobs.my-listings   GET    /app/job-club/my-listings
// ══════════════════════════════════════════════════════════════════

class JobController extends Controller
{
    public function __construct(private readonly JobService $jobService) {}

    // Browse active job listings
    public function index(Request $request): Response
    {
        $data = $this->jobService->index($request->user(), $request->only([
            'hub_id', 'job_type', 'search',
        ]));

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

    // Create new listing — form
    public function create(Request $request): Response
    {
        return Inertia::render('Member/JobClub/Jobs/Create', [
            'allHubs' => $this->jobService->index($request->user())['allHubs'],
        ]);
    }

    // Store new listing
    public function store(StoreJobListingRequest $request): \Illuminate\Http\RedirectResponse
    {
        $listing = $this->jobService->store($request->user(), $request->validated());

        return redirect()->route('member.jobs.show', $listing->id)
            ->with('success', __('cv.job_posted'));
    }

    // Single listing detail
    public function show(Request $request, JobListing $listing): Response
    {
        $data = $this->jobService->show($request->user(), $listing);

        return Inertia::render('Member/JobClub/Jobs/Show', $data);
    }

    // My listings — posted by me
    public function myListings(Request $request): Response
    {
        $data = $this->jobService->myListings($request->user());

        return Inertia::render('Member/JobClub/Jobs/MyListings', $data);
    }

    // Close a listing
    public function close(Request $request, JobListing $listing): \Illuminate\Http\JsonResponse
    {
        $this->jobService->close($request->user(), $listing);

        return response()->json(['status' => 'filled', 'message' => __('cv.job_closed')]);
    }

    // Reopen a listing
    public function reopen(Request $request, JobListing $listing): \Illuminate\Http\JsonResponse
    {
        $this->jobService->reopen($request->user(), $listing);

        return response()->json(['status' => 'active', 'message' => __('cv.job_reopened')]);
    }

    // Apply to a public listing
    public function apply(SendJobInterestRequest $request, JobListing $listing): \Illuminate\Http\JsonResponse
    {
        $notification = $this->jobService->apply(
            $request->user(),
            $listing,
            $request->input('message')
        );

        return response()->json([
            'notification_id' => $notification->id,
            'status'          => $notification->status,
            'message'         => __('cv.applied_success'),
        ]);
    }

    // Poster sends job directly to a CV profile from the pool
    public function sendDirect(SendJobInterestRequest $request, CvProfile $cv, JobListing $listing): \Illuminate\Http\JsonResponse
    {
        $notification = $this->jobService->sendDirect(
            $request->user(),
            $listing,
            $cv,
            $request->input('message')
        );

        return response()->json([
            'notification_id' => $notification->id,
            'status'          => $notification->status,
            'message'         => __('cv.interest_sent'),
        ]);
    }

    // Professional responds to an interest notification
    public function respond(RespondJobInterestRequest $request, JobNotification $notification): \Illuminate\Http\JsonResponse
    {
        $updated = $this->jobService->respond(
            $request->user(),
            $notification,
            $request->validated('response')
        );

        return response()->json([
            'status'  => $updated->status,
            'message' => $updated->status === 'accepted'
                ? __('cv.interest_accepted')
                : __('cv.interest_declined'),
        ]);
    }

    // Either side confirms consent to share contact details
    public function consent(Request $request, JobNotification $notification): \Illuminate\Http\JsonResponse
    {
        $result = $this->jobService->consent($request->user(), $notification);

        return response()->json(array_merge($result, [
            'message' => $result['status'] === 'connected'
                ? __('cv.now_connected')
                : __('cv.consent_recorded'),
        ]));
    }

    // Poster initiates contact with a specific applicant
    public function initiate(Request $request, JobNotification $notification): \Illuminate\Http\JsonResponse
    {
        $result = $this->jobService->initiateContact($request->user(), $notification);

        return response()->json(array_merge($result, [
            'message' => __('cv.contact_initiated'),
        ]));
    }

    // Professional's notifications inbox
    public function notifications(Request $request): Response
    {
        $data = $this->jobService->notifications($request->user());

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