<?php

declare(strict_types=1);

namespace App\Core;

final class Redirect
{
    public static function to(string $path): void
    {
        $path = trim($path);
        $base = Url::baseUri();

        $isAbsoluteUrl = preg_match('#^https?://#i', $path) === 1;
        $isAppRelative = $base !== '' && ($path === $base || str_starts_with($path, $base . '/'));
        $target = ($isAbsoluteUrl || $isAppRelative) ? $path : Url::to($path);

        header('Location: ' . $target);
        exit;
    }
}
