<?php

declare(strict_types=1);

namespace ImovelZapAi\Properties\Infrastructure\Http\Controllers\Web;

use Illuminate\Http\JsonResponse;
use ImovelZapAi\Properties\Application\Services\CepLookupService;
use RuntimeException;

final class CepLookupController
{
    public function show(string $cep, CepLookupService $service): JsonResponse
    {
        try {
            return response()->json($service->lookup($cep)->toArray());
        } catch (RuntimeException $exception) {
            return response()->json(
                ['message' => $exception->getMessage()],
                $exception->getMessage() === 'CEP não encontrado.' ? 404 : 422,
            );
        }
    }
}
