'use client';
import { useState, useEffect, useRef, useCallback } from 'react';
import { getDeploymentLogs } from '../app/actions/get-deployment-logs.js';
export function useDeploymentLogs(targetId) {
    const [logs, setLogs] = useState([]);
    const [isConnected, setIsConnected] = useState(false);
    const eventSourceRef = useRef(null);
    const cleanup = useCallback(() => {
        if (eventSourceRef.current) {
            eventSourceRef.current.close();
            eventSourceRef.current = null;
        }
        setIsConnected(false);
    }, []);
    useEffect(() => {
        if (!targetId) {
            setLogs([]);
            setIsConnected(false);
            return;
        }
        // Fetch initial logs
        getDeploymentLogs(targetId)
            .then((initialLogs) => {
            if (initialLogs) {
                setLogs(initialLogs);
            }
        })
            .catch(() => {
            // Initial fetch failed — logs stay empty, SSE will still connect
        });
        // Connect EventSource for live updates
        const es = new EventSource(`/api/deployment-logs?targetId=${targetId}`);
        eventSourceRef.current = es;
        es.onopen = () => {
            setIsConnected(true);
        };
        es.addEventListener('log', (event) => {
            const entry = JSON.parse(event.data);
            setLogs((prev) => [...prev, entry]);
        });
        es.onerror = () => {
            setIsConnected(false);
        };
        return () => {
            es.close();
            eventSourceRef.current = null;
        };
    }, [targetId, cleanup]);
    return { logs, isConnected };
}
