<?php

namespace Lucent\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Lucent\Account\AccountService;
use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService;

readonly class AuthMiddleware
{
    public function __construct(
        private AuthService $authService,
        private ChannelService $channelService
    )
    {
    }

    public function handle(Request $request, Closure $next)
    {
        if (!$this->authService->isLoggedIn()) {
            return redirect($this->channelService->channel->lucentUrl . "/login");
        }

        $this->authService->refreshSession();


        return $next($request);
    }
}
