<?php
/**
 * URL Helper
 * Clean routing and asset path resolver
 */

class Url {
    private static ?string $baseUrl = null;
    private static ?string $basePath = null;

    private static function init(): void {
        if (self::$baseUrl === null) {
            $config = require __DIR__ . '/../../config/app.php';
            self::$baseUrl = rtrim($config['app_url'], '/');
            self::$basePath = rtrim($config['base_path'], '/');
        }
    }

    /**
     * Generate relative or absolute application URL
     */
    public static function to(string $path = ''): string {
        self::init();
        $path = '/' . ltrim($path, '/');
        if ($path === '/') {
            return self::$basePath !== '' ? self::$basePath : '/';
        }
        return self::$basePath . $path;
    }

    /**
     * Generate asset URL
     */
    public static function asset(string $path): string {
        self::init();
        $path = '/' . ltrim($path, '/');
        return self::$basePath . $path;
    }

    /**
     * Redirect to another internal path
     */
    public static function redirect(string $path): void {
        $url = self::to($path);
        header("Location: " . $url);
        exit;
    }
}

/**
 * Global URL shortcuts
 */
function url(string $path = ''): string {
    return Url::to($path);
}

function asset(string $path): string {
    return Url::asset($path);
}

/**
 * Resolve product image URL with intelligent fallback
 */
function product_image_url(?string $path): string {
    if (empty($path)) {
        return asset('/assets/images/brand/logo1-blue.png');
    }
    $path = trim($path);
    if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
        return $path;
    }
    if (str_starts_with($path, 'assets/') || str_starts_with($path, '/assets/')) {
        return asset('/' . ltrim($path, '/'));
    }
    if (str_starts_with($path, 'uploads/') || str_starts_with($path, '/uploads/')) {
        return asset('/' . ltrim($path, '/'));
    }
    // Default product image stored in uploads/products/
    return asset('/uploads/products/' . ltrim($path, '/'));
}

/**
 * Resolve store image URL with fallback
 */
function store_image_url(?string $path, string $type = 'logo'): string {
    if (empty($path)) {
        return $type === 'cover' ? '' : asset('/assets/images/brand/logo1-blue.png');
    }
    $path = trim($path);
    if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
        return $path;
    }
    if (str_starts_with($path, 'assets/') || str_starts_with($path, '/assets/')) {
        return asset('/' . ltrim($path, '/'));
    }
    if (str_starts_with($path, 'uploads/') || str_starts_with($path, '/uploads/')) {
        return asset('/' . ltrim($path, '/'));
    }
    // Default store image stored in uploads/stores/
    return asset('/uploads/stores/' . ltrim($path, '/'));
}

/**
 * Resolve review image URL
 */
function review_image_url(?string $path): string {
    if (empty($path)) {
        return '';
    }
    $path = trim($path);
    if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
        return $path;
    }
    if (str_starts_with($path, 'assets/') || str_starts_with($path, '/assets/')) {
        return asset('/' . ltrim($path, '/'));
    }
    if (str_starts_with($path, 'uploads/') || str_starts_with($path, '/uploads/')) {
        return asset('/' . ltrim($path, '/'));
    }
    // Review images stored in uploads/reviews/
    return asset('/uploads/reviews/' . ltrim($path, '/'));
}

function redirect(string $path): void {
    Url::redirect($path);
}
