/**
 * Nexus Prime - NexusNet Relay
 *
 * MVP Cross-machine knowledge federation using a shared GitHub Gist as a pub/sub mechanism.
 * Note: Requires `GITHUB_TOKEN` and `NEXUSNET_GIST_ID` environment variables.
 *
 * Phase: 8G (NexusNet Relay)
 */
import { randomUUID } from 'crypto';
// ─────────────────────────────────────────────────────────────────────────────
// NexusNet Relay
// ─────────────────────────────────────────────────────────────────────────────
export class NexusNetRelay {
    sourceId;
    lastError;
    lastSyncAt;
    lastPublishAt;
    constructor() {
        // In a real system this would persist or be tied to install ID
        this.sourceId = `nexus_agent_${randomUUID().slice(0, 8)}`;
    }
    getConfig() {
        const token = process.env.GITHUB_TOKEN;
        const gistId = process.env.NEXUSNET_GIST_ID;
        return { token, gistId, authReady: !!(token && gistId) };
    }
    getStatus() {
        const config = this.getConfig();
        return {
            configured: config.authReady,
            mode: config.authReady ? 'live' : 'degraded',
            gistId: config.gistId,
            lastError: this.lastError,
            lastSyncAt: this.lastSyncAt,
            lastPublishAt: this.lastPublishAt,
        };
    }
    /**
     * Publish a message to the shared Gist network.
     */
    async publish(type, payload, ttl = 86400) {
        const message = {
            id: randomUUID(),
            type,
            sourceId: this.sourceId,
            payload,
            timestamp: Date.now(),
            ttl
        };
        const config = this.getConfig();
        const jsonString = JSON.stringify(message);
        const bytes = Buffer.byteLength(jsonString, 'utf8');
        if (!config.authReady) {
            this.lastError = 'GITHUB_TOKEN or NEXUSNET_GIST_ID missing';
            return { id: message.id, bytes, configured: false, mode: 'degraded' };
        }
        try {
            // Read current
            const response = await fetch(`https://api.github.com/gists/${config.gistId}`, {
                headers: {
                    'Authorization': `Bearer ${config.token}`,
                    'Accept': 'application/vnd.github.v3+json'
                }
            });
            if (!response.ok)
                throw new Error(`Fetch failed: ${response.statusText}`);
            const gistData = (await response.json());
            const fileContent = gistData.files?.['nexusnet.json']?.content || '[]';
            const messages = JSON.parse(fileContent);
            // Filter expired
            const now = Date.now();
            const valid = messages.filter(m => now - m.timestamp < m.ttl * 1000);
            // Append
            valid.push(message);
            // Write back
            const updateResponse = await fetch(`https://api.github.com/gists/${config.gistId}`, {
                method: 'PATCH',
                headers: {
                    'Authorization': `Bearer ${config.token}`,
                    'Accept': 'application/vnd.github.v3+json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    files: {
                        'nexusnet.json': { content: JSON.stringify(valid, null, 2) }
                    }
                })
            });
            if (!updateResponse.ok)
                throw new Error(`Update failed: ${updateResponse.statusText}`);
            this.lastError = undefined;
            this.lastPublishAt = Date.now();
            return { id: message.id, bytes, configured: true, mode: 'live' };
        }
        catch (error) {
            this.lastError = String(error?.message ?? error);
            throw new Error(`Failed to publish to NexusNet: ${error.message}`);
        }
    }
    /**
     * Sync messages from the shared Gist network.
     */
    async sync() {
        const config = this.getConfig();
        if (!config.authReady) {
            this.lastError = 'GITHUB_TOKEN or NEXUSNET_GIST_ID missing';
            return [];
        }
        try {
            const response = await fetch(`https://api.github.com/gists/${config.gistId}`, {
                headers: {
                    'Authorization': `Bearer ${config.token}`,
                    'Accept': 'application/vnd.github.v3+json',
                    'Cache-Control': 'no-cache, no-store, must-revalidate'
                }
            });
            if (!response.ok)
                throw new Error(`Fetch failed: ${response.statusText}`);
            const gistData = (await response.json());
            const fileContent = gistData.files?.['nexusnet.json']?.content || '[]';
            const messages = JSON.parse(fileContent);
            // Filter out our own messages and expired ones
            const now = Date.now();
            this.lastError = undefined;
            this.lastSyncAt = now;
            return messages.filter(m => m.sourceId !== this.sourceId && (now - m.timestamp < m.ttl * 1000));
        }
        catch (error) {
            this.lastError = String(error?.message ?? error);
            throw new Error(`Failed to sync from NexusNet: ${error.message}`);
        }
    }
}
export const nexusNetRelay = new NexusNetRelay();
