<?php

declare(strict_types=1);

function seoscope_base_path(): string
{
    $c = $GLOBALS['seoscope_config'] ?? [];
    $app = $c['app'] ?? [];
    $raw = (string) ($app['base_path'] ?? '');
    $raw = trim($raw, '/');
    return $raw === '' ? '' : '/' . $raw;
}

function seoscope_url(string $path): string
{
    $path = $path === '' ? '/' : ($path[0] === '/' ? $path : '/' . $path);
    $base = seoscope_base_path();
    if ($base === '') {
        return $path;
    }
    return $base . ($path === '/' ? '' : $path);
}

/**
 * Absolute OAuth redirect URI for Google Cloud Console ("Authorized redirect URIs").
 * Uses app.url and app.base_path from $GLOBALS['seoscope_config'].
 */
function seoscope_google_oauth_redirect_uri(): string
{
    $c = $GLOBALS['seoscope_config'] ?? [];
    $app = is_array($c['app'] ?? null) ? $c['app'] : [];
    $appUrl = trim((string) ($app['url'] ?? ''));
    $basePath = (string) ($app['base_path'] ?? '');
    $basePath = $basePath === '' ? '' : '/' . trim($basePath, '/');
    if ($appUrl === '') {
        return '';
    }
    return rtrim($appUrl, '/') . $basePath . '/oauth/google/callback';
}

/**
 * Absolute https URL for a path handled by SeoScope (honours app.base_path).
 * If app.url is not set, returns the same path as seoscope_url() (relative).
 */
function seoscope_absolute_app_url(string $path): string
{
    $path = $path === '' ? '/' : ($path[0] === '/' ? $path : '/' . $path);
    $relative = seoscope_url($path);
    $c = $GLOBALS['seoscope_config'] ?? [];
    $app = is_array($c['app'] ?? null) ? $c['app'] : [];
    $appUrl = trim((string) ($app['url'] ?? ''));
    if ($appUrl === '') {
        return $relative;
    }
    return rtrim($appUrl, '/') . $relative;
}

/**
 * One-line curl example for hosting cron / Plesk (GET with loops=4).
 */
function seoscope_cron_curl_example_line(string $cronAbsoluteOrRelativeUrl): string
{
    $u = trim($cronAbsoluteOrRelativeUrl);
    if ($u === '') {
        return '';
    }

    // Ensure the example includes loops=4 by default (consistent with on-page help).
    if (!preg_match('~[?&]loops=~', $u)) {
        $sep = str_contains($u, '?') ? '&' : '?';
        $u .= $sep . 'loops=4';
    }

    $q = escapeshellarg($u);

    return '/usr/bin/curl -fsS --max-time 120 ' . $q . ' > /dev/null 2>&1';
}

function seoscope_h(string $s): string
{
    return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
}

/**
 * Normalize a GSC property site URL for display.
 *
 * Examples:
 * - https://alunia.it/ -> alunia.it
 * - http://www.example.com/ -> www.example.com
 * - sc-domain:example.com -> example.com
 */
function seoscope_prop_label(string $siteUrl): string
{
    $s = trim($siteUrl);
    if ($s === '') {
        return '';
    }
    if (str_starts_with($s, 'sc-domain:')) {
        $d = rtrim(substr($s, strlen('sc-domain:')), '/');
        $d = preg_replace('#^www\.#i', '', $d) ?? $d;
        return $d;
    }
    $s = rtrim($s, '/');
    $host = parse_url($s, PHP_URL_HOST);
    if (is_string($host) && $host !== '') {
        $host = preg_replace('#^www\.#i', '', $host) ?? $host;
        return $host;
    }
    $s = preg_replace('#^https?://#i', '', $s) ?? $s;
    $s = preg_replace('#^www\.#i', '', $s) ?? $s;
    return rtrim($s, '/');
}
