<?php

namespace App\Http\Middleware;

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

class EnsureMexcoPortal
{
    public function __construct(private CustomerAuthService $auth) {}

    public function handle(Request $request, Closure $next): Response
    {
        $user = $this->auth->currentUser($request);

        if (! $user) {
            return redirect()->route('mexco.login')
                ->with('error', 'Please sign in to My MeXCo.');
        }

        $request->attributes->set('mexcoPortalUser', $user);

        return $next($request);
    }
}
