<?php

declare(strict_types=1);

namespace App\Http\Middleware;

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

final class EnsureDBErpIsNotInstalled
{
    public function handle(Request $request, Closure $next): Response
    {
        if (app(DBErpInstallStatus::class)->isInstalled()) {
            $panelPath = trim((string) config('dberp.admin.panel_path', ''), '/');

            if ($panelPath === '' || $panelPath === 'admin') {
                return redirect('/admin');
            }

            return redirect()->route('filament.admin.auth.login');
        }

        return $next($request);
    }
}
