<?php
namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Support\ApiResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Throwable;

final class HealthController extends Controller
{
    public function __invoke()
    {
        $checks = ['database'=>false,'redis'=>false];
        try { DB::select('select 1'); $checks['database'] = true; } catch (Throwable) {}
        try { Cache::store('redis')->put('health:probe', 'ok', 10); $checks['redis'] = Cache::store('redis')->get('health:probe') === 'ok'; } catch (Throwable) {}
        $ok = !in_array(false, $checks, true);
        return ApiResponse::success(['status'=>$ok?'ok':'degraded','checks'=>$checks], $ok?200:503);
    }
}
