'use client';

import { useEffect } from 'react';
import { usePathname } from 'next/navigation';

export function Analytics() {
    const pathname = usePathname();

    useEffect(() => {
        // Send beacon on mount and path change
        const sendBeacon = async () => {
            try {
                // Point to the Admin Dashboard API
                // In prod, this URL needs to be the absolute URL of admin_dash
                // For local docker, standard localhost access might work if CORS allows,
                // or we route through a proxy. 
                // For this personal setup, let's assume they are on same domain or localhost.
                await fetch('http://localhost:3000/api/track', { // TODO: Make this configurable
                    method: 'POST',
                    body: JSON.stringify({ path: pathname, timestamp: Date.now() }),
                    headers: { 'Content-Type': 'application/json' },
                    keepalive: true,
                });
            } catch (e) {
                // Fail silently
                console.error('Analytics fail', e);
            }
        };

        sendBeacon();
    }, [pathname]);

    return null;
}
