/**
 * adminClient.js
 * Webex client for privileged / admin / telephony operations.
 *
 * This is intentionally separate from botClient.js.
 *
 * Required for:
 *   - DECT network/handset/base station lookups
 *   - Device inventory for people (phones assigned to store users)
 *   - Any other Webex API calls that require broader admin or telephony scopes
 *
 * These operations typically need a different token (not the ServChan bot token)
 * with the appropriate Webex admin/telephony permissions.
 *
 * During the 2026-05-28 cleanup, the old duplicated code from
 * src/integrations/webex/client.js (previously contained DECT/telephony code)
 * should be migrated here when the privileged token is available via env/config.
 *
 * For now this is a skeleton so the architecture is clear.
 */

import axios from 'axios';
import { logger } from '../../utils/logger.js';

export class WebexAdminClient {
  constructor(token, baseURL = 'https://webexapis.com/v1') {
    if (!token) {
      logger('webex:adminClient', 'No token provided — privileged operations will fail', 'warn');
    }

    this.axios = axios.create({
      baseURL,
      timeout: 15000,
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
    });
  }

  // TODO: Move the DECT / telephony functions here from the old client.js
  // Examples that belong in this client:
  //   - getPersonIdByEmail
  //   - getDevicesForPerson
  //   - getDectNetworkId
  //   - getDectHandsets / getDectBasestations
  //   - getWebexPhonesForStore
  //   - etc.

  async getPersonIdByEmail(email) {
    // Placeholder — implement using this.axios
    logger('webex:adminClient', 'getPersonIdByEmail not yet implemented in new adminClient');
    throw new Error('Not implemented — migrate from legacy webex/client.js');
  }

  // Add other admin-only methods as they are migrated...
}

let cachedAdminClient = null;

export function getAdminClient() {
  if (!cachedAdminClient) {
    // Expect a separate privileged token (never the bot token)
    const token = process.env.WEBEX_ADMIN_TOKEN || process.env.WEBEX_TELEPHONY_TOKEN;
    if (!token) {
      logger('webex:adminClient', 'WEBEX_ADMIN_TOKEN (or WEBEX_TELEPHONY_TOKEN) not set', 'error');
      throw new Error('Missing privileged Webex token for admin operations');
    }
    cachedAdminClient = new WebexAdminClient(token);
  }
  return cachedAdminClient;
}

export default getAdminClient;