<?php

/**
 * Handle the authentication for the API layer
 */

namespace App\Http\Middleware;

use App\Contracts\Middleware;
use App\Filament\System\Installer;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\HttpFoundation\Response;

/**
 * Check the app.key to see whether we're installed or not
 *
 * If the default key is set and we're not in any of the installer routes
 * show the message that we need to be installed
 */
class InstalledCheck implements Middleware
{
    public function handle(Request $request, Closure $next): Response
    {
        // If we're in the installer, skip this
        // Also skip if this is a livewire update (might be called from the system)
        if ($request->is('system*') || request()->is('livewire-*/update')) {
            return $next($request);
        }

        try {
            DB::connection()->getPdo();
            if (!Schema::hasTable('users') || User::count() === 0) {
                return redirect('/system/install');
            }
        } catch (\Exception) {
            return redirect('/system/install');
        }

        return $next($request);
    }
}
