<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Auth\SessionManager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
 * `/` — bounce: signed in users land on `/app/me`, guests on `/login`.
 */
final class HomeController
{
    public function __construct(private readonly SessionManager $sessions)
    {
    }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $location = $this->sessions->getUser() === null ? '/login' : '/app/dashboard';

        return $response->withStatus(302)->withHeader('Location', $location);
    }
}
