<?php
namespace PortalPeople\AuthRedirect\Plugin;

use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\RequestInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\UrlInterface;

class AccountRedirectPlugin
{
    private Session $customerSession;
    private UrlInterface $urlBuilder;

    public function __construct(Session $customerSession, UrlInterface $urlBuilder)
    {
        $this->customerSession = $customerSession;
        $this->urlBuilder = $urlBuilder;
    }

    public function aroundGetRedirect(AccountRedirect $subject, \Closure $proceed): string
    {
        if ($this->customerSession->getIsAuthRedirect()) {
            $this->customerSession->unsIsAuthRedirect();
            return $this->urlBuilder->getUrl('checkout');
        }

        $result = $proceed();
        return is_string($result) ? $result : $this->urlBuilder->getUrl('customer/account');
    }
}
