<?php

namespace App\Http\Controllers\Api\Administrator;

use App\Http\Controllers\Controller;
use App\Http\Requests\EmergencyContacts\EmergencyContactUpdateRequest;
use App\Http\Resources\EmergencyContacts\EmergencyContactGroupResource;
use App\Http\Resources\EmergencyContacts\EmergencyContactResource;
use App\Services\EmergencyContacts\EmergencyContactCrudService;
use App\Services\EmergencyContacts\EmergencyContactDirectoryService;
use Illuminate\Http\JsonResponse;

class EmergencyContactController extends Controller
{
    public function __construct(
        private EmergencyContactDirectoryService $directoryService,
        private EmergencyContactCrudService $crudService
    ) {
    }

    public function index(): JsonResponse
    {
        $groups = $this->directoryService->groups();

        return response()->json([
            'ok' => true,
            'groups' => EmergencyContactGroupResource::collection($groups),
        ]);
    }

    public function update(EmergencyContactUpdateRequest $request, int $contactId): JsonResponse
    {
        $contact = $this->crudService->update($contactId, $request->validated());

        return response()->json([
            'ok' => true,
            'contact' => new EmergencyContactResource($contact),
        ]);
    }

    public function destroy(int $contactId): JsonResponse
    {
        $this->crudService->delete($contactId);

        return response()->json(['ok' => true]);
    }
}
