import { headers } from 'next/headers';

/**
 * Retrieves the client's IP address from the request headers.
 * It checks for standard proxy headers first.
 * In a local development environment, this will typically return '127.0.0.1'.
 * In a production environment (like Firebase App Hosting), it will return the user's actual IP.
 */
export async function getIpAddress(): Promise<string> {
    const headersList = await headers();
    const forwardedFor = headersList.get('x-forwarded-for');
    if (forwardedFor) {
        // The first IP in the list is the original client IP
        return forwardedFor.split(',')[0].trim();
    }

    const realIp = headersList.get('x-real-ip');
    if (realIp) {
        return realIp.trim();
    }

    // Fallback for local development
    return '127.0.0.1';
}
