<?php

namespace App\Http\Middleware;

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

/**
 * Blocks every route that only makes sense for a business which registers
 * customers — chiefly the whole credit-note surface.
 *
 * When a tenant runs in walk-in-only mode there is no customer to hold a
 * credit, so a credit note cannot be issued, claimed, or redeemed. Hiding the
 * menu entry is not enough on its own: the routes stay reachable by URL, so the
 * switch is enforced here rather than trusted to the frontend.
 */
class EnsureCustomersEnabled
{
    public function __construct(private readonly PosSettings $posSettings) {}

    public function handle(Request $request, Closure $next): Response
    {
        if ($this->posSettings->walkinOnly()) {
            abort(404, 'Credit notes are disabled: this business is configured for walk-in customers only.');
        }

        return $next($request);
    }
}
