import api from './api';

const base = '/network-monitor';

const networkMonitorService = {
    getAll:     ()        => api.get(base).then(r => r.data.data),
    getStats:   ()        => api.get(`${base}/statistics`).then(r => r.data.data),
    getChecks:  (id, h)   => api.get(`${base}/${id}/checks?hours=${h}`).then(r => r.data.data),
    create:     (data)    => api.post(base, data).then(r => r.data.data),
    update:     (id, d)   => api.put(`${base}/${id}`, d).then(r => r.data.data),
    delete:     (id)      => api.delete(`${base}/${id}`),
    checkNow:   (id)      => api.post(`${base}/${id}/check-now`).then(r => r.data.data),

    discover:      (subnet) => api.post(`${base}/discover`, { subnet }, { timeout: 120000 }).then(r => r.data),
    getUptimeStats: ()       => api.get(`${base}/uptime-stats`).then(r => r.data.data),

    createSSE: () => {
        const token = localStorage.getItem('token');
        const apiBase = process.env.REACT_APP_API_URL || '/api';
        return new EventSource(`${apiBase}/network-monitor/sse?token=${token}`);
    },
};

export default networkMonitorService;
