<?php

namespace App\Http\Controllers\Api\Administrator;

use App\Http\Controllers\Controller;
use App\Http\Requests\EmergencyContacts\EmergencyContactParentRequest;
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;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

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

    public function index(Request $request): JsonResponse
    {
        $validator = Validator::make($request->query->all(), [
            'parent_id' => ['nullable', 'integer', 'min:1'],
            'parent_ids' => ['nullable', 'array'],
            'parent_ids.*' => ['integer', 'min:1'],
        ]);

        if ($validator->fails()) {
            return response()->json([
                'message' => 'Validation failed.',
                'errors' => $validator->errors(),
            ], 422);
        }

        $payload = $validator->validated();
        $parentIds = [];
        if (!empty($payload['parent_ids'])) {
            $parentIds = array_values(array_unique(array_map('intval', $payload['parent_ids'])));
        }
        if (!empty($payload['parent_id'])) {
            $parentIds[] = (int) $payload['parent_id'];
        }
        $parentIds = array_values(array_unique(array_filter($parentIds)));

        $groups = $this->directoryService->groups($parentIds ?: null);

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

    public function show(EmergencyContactParentRequest $request, int $contactId): JsonResponse
    {
        $parentId = (int) $request->validated()['parent_id'];
        $contact = $this->crudService->findForParent($contactId, $parentId);

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

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

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

    public function destroy(EmergencyContactParentRequest $request, int $contactId): JsonResponse
    {
        $parentId = (int) $request->validated()['parent_id'];
        $this->crudService->delete($contactId, $parentId);

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