const NEXUS_AUTH_URL = process.env.NEXUS_AUTH_URL || 'https://aethex.tech';
const NEXUS_SERVICE_URL = process.env.NEXUS_SERVICE_URL || 'https://aethex.cloud';

class NexusClient {
  constructor() {
    this.authUrl = NEXUS_AUTH_URL;
    this.serviceUrl = NEXUS_SERVICE_URL;
  }

  async getToken(credentials) {
    const response = await fetch(`${this.authUrl}/api/passport/token`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(credentials)
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Token request failed' }));
      throw new Error(error.error || 'Failed to get token');
    }
    
    return response.json();
  }

  async refreshToken(refreshToken) {
    const response = await fetch(`${this.authUrl}/api/passport/refresh`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ refresh_token: refreshToken })
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Refresh failed' }));
      throw new Error(error.error || 'Failed to refresh token');
    }
    
    return response.json();
  }

  async scrubText(text, accessToken) {
    const response = await fetch(`${this.serviceUrl}/api/sentinel/scrub`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${accessToken}`
      },
      body: JSON.stringify({ text })
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Scrub failed' }));
      throw new Error(error.error || 'Failed to scrub text');
    }
    
    return response.json();
  }

  async checkSelfIP(accessToken) {
    const response = await fetch(`${this.serviceUrl}/api/sentinel/check-self`, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${accessToken}`
      }
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'IP check failed' }));
      throw new Error(error.error || 'Failed to check IP');
    }
    
    return response.json();
  }

  async logActivity(activityData, accessToken) {
    const response = await fetch(`${this.serviceUrl}/api/sentinel/activity/log`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${accessToken}`
      },
      body: JSON.stringify(activityData)
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Activity log failed' }));
      throw new Error(error.error || 'Failed to log activity');
    }
    
    return response.json();
  }

  async checkIP(ip, accessToken) {
    const response = await fetch(`${this.serviceUrl}/api/blacklist/check/${encodeURIComponent(ip)}`, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${accessToken}`
      }
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Blacklist check failed' }));
      throw new Error(error.error || 'Failed to check IP blacklist');
    }
    
    return response.json();
  }

  async getUserSnapshot(userId, accessToken) {
    const response = await fetch(`${this.serviceUrl}/api/activity/user-snapshot/${encodeURIComponent(userId)}`, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${accessToken}`
      }
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Snapshot fetch failed' }));
      throw new Error(error.error || 'Failed to get user snapshot');
    }
    
    return response.json();
  }

  async getPublicStats() {
    const response = await fetch(`${this.serviceUrl}/api/overview/public-stats`, {
      method: 'GET'
    });
    
    if (!response.ok) {
      const error = await response.json().catch(() => ({ error: 'Stats fetch failed' }));
      throw new Error(error.error || 'Failed to get public stats');
    }
    
    return response.json();
  }
}

module.exports = { NexusClient };
