<?php

/**
 * URL and Path Helper Functions
 *
 * Provides environment-aware URL generation, path resolving, and asset helpers.
 *
 * @package Inveetaire
 */

if (!function_exists('base_path')) {
    /**
     * Get the detected user-facing base path (subdirectory prefix) of the application.
     * Returns a leading-slash path (e.g. '/inveetaire') or empty string '' if at root.
     *
     * @return string
     */
    function base_path(): string
    {
        static $basePath = null;
        if ($basePath !== null) {
            return $basePath;
        }

        if (empty($_SERVER['SCRIPT_NAME'])) {
            return '';
        }

        $script = $_SERVER['SCRIPT_NAME'];
        if (strpos($script, '/public/index.php') !== false) {
            $basePath = str_replace('/public/index.php', '', $script);
        } elseif (strpos($script, '/index.php') !== false) {
            $basePath = str_replace('/index.php', '', $script);
        } else {
            $basePath = rtrim(dirname($script), '/\\');
        }

        $basePath = rtrim($basePath, '/\\');
        return $basePath;
    }
}

if (!function_exists('url')) {
    /**
     * Generate a fully qualified absolute URL with protocol, host, and detected base path prefix.
     *
     * @param string $path Target path relative to application root (e.g. '/login').
     * @return string Fully qualified absolute URL.
     */
    function url(string $path = ''): string
    {
        if (strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0) {
            return $path;
        }

        $path = '/' . ltrim($path, '/');

        if (empty($_SERVER['HTTP_HOST'])) {
            $appUrl = rtrim(env('APP_URL', 'http://localhost/inveetaire/public'), '/');
            return $appUrl . $path;
        }

        $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'];
        $base = base_path();

        return $protocol . '://' . $host . $base . $path;
    }
}

if (!function_exists('asset')) {
    /**
     * Generate a root-relative path to a public asset, prepending the detected base path.
     *
     * @param string $path Path relative to public directory (e.g. '/assets/css/app.css').
     * @return string Root-relative path.
     */
    function asset(string $path): string
    {
        $path = '/' . ltrim($path, '/');
        return base_path() . $path;
    }
}

if (!function_exists('route')) {
    /**
     * Generate an absolute URL for a route with replaced parameter placeholders.
     *
     * @param string $path Route path with placeholders (e.g. '/{slug}/i/{guest_token}').
     * @param array  $params Key-value mapping of placeholders to replace.
     * @return string Fully qualified absolute URL.
     */
    function route(string $path, array $params = []): string
    {
        foreach ($params as $key => $value) {
            $path = str_replace('{' . $key . '}', (string) $value, $path);
        }
        return url($path);
    }
}
