<?php

namespace App\Http\Controllers\Api\System;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Resources\System\HealthStatusResource;
use App\Services\System\HealthCheckService;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class HealthController extends BaseApiController
{
    public function __construct(private HealthCheckService $service)
    {
        parent::__construct();
    }

    public function index(): JsonResponse
    {
        $payload = $this->service->check();
        $status = $payload['ok'] ? Response::HTTP_OK : Response::HTTP_SERVICE_UNAVAILABLE;

        return $this->success([
            'health' => new HealthStatusResource($payload),
        ], 'Health check.', $status);
    }
}
