/**
 * Détecte automatiquement l'URL de base du serveur Symfony (sans /api)
 */
export function getServerUrl(): string {
    // En production, toujours utiliser l'URL de production
    if (process.env.NODE_ENV === 'production') {
        return process.env.NEXT_PUBLIC_API_URL?.replace('/api', '') || 'https://api.marylineapps.com';
    }

    // En développement, détecter si on est sur localhost ou sur le réseau
    if (typeof window !== 'undefined') {
        const hostname = window.location.hostname;

        // Si on accède via localhost ou 127.0.0.1 (depuis le PC)
        if (hostname === 'localhost' || hostname === '127.0.0.1') {
            return 'http://localhost:8000';
        }

        // Si on accède via l'IP du réseau (depuis mobile)
        if (hostname.match(/^192\.168\.\d+\.\d+$/) || hostname.match(/^10\.0\.\d+\.\d+$/)) {
            return `http://${hostname}:8000`;
        }

        // Fallback : utiliser l'hostname actuel
        return `http://${hostname}:8000`;
    }

    // Côté serveur (SSR), utiliser la variable d'environnement
    return process.env.NEXT_PUBLIC_API_URL?.replace('/api', '') || 'http://localhost:8000';
}

/**
 * Détecte automatiquement l'URL de l'API selon l'environnement
 * - Depuis localhost (PC) : http://localhost:8000/api
 * - Depuis le réseau local (mobile) : http://192.168.1.28:8000/api
 */
export function getApiUrl(): string {
    // En production, toujours utiliser l'URL de production
    if (process.env.NODE_ENV === 'production') {
        return process.env.NEXT_PUBLIC_API_URL || 'https://api.marylineapps.com/api';
    }

    // En développement, détecter si on est sur localhost ou sur le réseau
    if (typeof window !== 'undefined') {
        const hostname = window.location.hostname;

        console.log('🔍 Détection hostname:', hostname);

        // Si on accède via localhost ou 127.0.0.1 (depuis le PC)
        if (hostname === 'localhost' || hostname === '127.0.0.1') {
            console.log('✅ Mode PC détecté → API sur localhost');
            return 'http://localhost:8000/api';
        }

        // Si on accède via l'IP du réseau (depuis mobile)
        if (hostname.match(/^192\.168\.\d+\.\d+$/) || hostname.match(/^10\.0\.\d+\.\d+$/)) {
            console.log('✅ Mode mobile détecté → API sur', hostname);
            return `http://${hostname}:8000/api`;
        }

        // Fallback : utiliser l'hostname actuel
        console.log('⚠️ Hostname non reconnu, utilisation de:', hostname);
        return `http://${hostname}:8000/api`;
    }

    // Côté serveur (SSR), utiliser la variable d'environnement
    return process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api';
}

/**
 * Construit l'URL complète d'une image uploadée
 * Si l'image est déjà une URL complète (http/https), la retourne telle quelle
 * Sinon, construit l'URL vers le serveur Symfony
 */
export function getImageUrl(imagePath: string): string {
    // Si c'est déjà une URL complète, la retourner
    if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
        return imagePath;
    }

    // Sinon, construire l'URL vers le serveur Symfony
    const serverUrl = getServerUrl();
    // Ajouter /uploads/ si le chemin ne commence pas par /
    const path = imagePath.startsWith('/') ? imagePath : `/uploads/${imagePath}`;
    return `${serverUrl}${path}`;
}