<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\MissionService;
use App\Http\Requests\Admin\StoreMissionRequest;
use App\Http\Requests\Admin\ChangerStatutMissionRequest;
use App\Http\Requests\Admin\StoreRemplacementRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MissionController extends Controller
{
    public function __construct(
        private readonly MissionService $missionService
    ) {}

    public function index(Request $request): JsonResponse
    {
        $query = \App\Models\Mission::query();

        if ($request->has('statut')) {
            $query->where('statut', $request->statut);
        }

        $missions = $query->with(['candidat.user', 'besoin.client'])
            ->paginate(15);

        return response()->json($missions);
    }

    public function store(StoreMissionRequest $request): JsonResponse
    {
        $mission = $this->missionService->creerMission($request->validated(), auth()->user());

        return response()->json($mission, 201);
    }

    public function show(string $reference): JsonResponse
    {
        $mission = \App\Models\Mission::where('reference', $reference)
            ->with(['candidat.user', 'client', 'besoin'])
            ->firstOrFail();

        return response()->json($mission);
    }

    public function changerStatut(ChangerStatutMissionRequest $request, string $reference): JsonResponse
    {
        $mission = $this->missionService->changerStatutMission(
            $reference,
            $request->statut,
            $request->motif,
            auth()->user()
        );

        return response()->json($mission);
    }

    public function destroy(string $reference): JsonResponse
    {
        $this->missionService->supprimerMission($reference);

        return response()->json(['message' => 'Mission supprimée avec succès']);
    }

    public function descentes(string $reference): JsonResponse
    {
        $mission = \App\Models\Mission::where('reference', $reference)->firstOrFail();
        $descentes = $mission->descentesTerrain()->with('effectueParUser')->get();

        return response()->json($descentes);
    }

    public function signalements(string $reference): JsonResponse
    {
        $mission = \App\Models\Mission::where('reference', $reference)->firstOrFail();
        $signalements = $mission->signalements()->with(['client'])->get();

        return response()->json($signalements);
    }

    public function remplacements(string $reference): JsonResponse
    {
        $mission = \App\Models\Mission::where('reference', $reference)->firstOrFail();
        $remplacements = $mission->remplacements()->with(['ancienCandidat.user', 'nouveauCandidat.user'])->get();

        return response()->json($remplacements);
    }

    public function storeRemplacement(StoreRemplacementRequest $request, string $reference): JsonResponse
    {
        $remplacement = $this->missionService->creerRemplacement($reference, $request->validated(), auth()->user());

        return response()->json($remplacement, 201);
    }
}
