<?php

namespace App\Http\Middleware;

use App\Services\InstallerService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Gates the API behind installation with a clean JSON error instead of a
 * redirect (edge devices can't follow a browser redirect to /install).
 */
class EnsureApiInstalled
{
    public function handle(Request $request, Closure $next): Response
    {
        if (app(InstallerService::class)->isInstalled()) {
            return $next($request);
        }

        return response()->json([
            'error' => ['code' => 'not_installed', 'message' => 'This application has not completed the /install setup wizard yet.'],
        ], 503);
    }
}
