// node native
import * as dns from 'node:dns';
import * as fs from 'node:fs';
import * as crypto from 'node:crypto';
import * as http from 'node:http';
import * as net from 'node:net';
import * as os from 'node:os';
import * as path from 'node:path';
import * as tls from 'node:tls';
import * as util from 'node:util';

export {
  dns,
  fs,
  crypto,
  http,
  net,
  os,
  path,
  tls,
  util,
}

// @serve.zone scope
import * as servezoneInterfaces from '@serve.zone/interfaces';
import * as remoteingress from '@serve.zone/remoteingress';

export {
  servezoneInterfaces,
  remoteingress,
}

// @api.global scope
import * as typedrequest from '@api.global/typedrequest';
import * as typedserver from '@api.global/typedserver';
import * as typedsocket from '@api.global/typedsocket';

export {
  typedrequest,
  typedserver,
  typedsocket,
}

// @idp.global scope
import * as idpSdkServer from '@idp.global/sdk/server';

export {
  idpSdkServer,
}

// @push.rocks scope
import * as projectinfo from '@push.rocks/projectinfo';
import * as qenv from '@push.rocks/qenv';
import * as smartacme from '@push.rocks/smartacme';
import * as smartdata from '@push.rocks/smartdata';
import * as smartdns from '@push.rocks/smartdns';
import * as smartfs from '@push.rocks/smartfs';
import * as smartguard from '@push.rocks/smartguard';
import * as smartjwt from '@push.rocks/smartjwt';
import * as smartlog from '@push.rocks/smartlog';
import * as smartmetrics from '@push.rocks/smartmetrics';
import * as smartmta from '@push.rocks/smartmta';
import * as smartdb from '@push.rocks/smartdb';
import * as smartnetwork from '@push.rocks/smartnetwork';
import * as smartpath from '@push.rocks/smartpath';
import * as smartproxy from '@push.rocks/smartproxy';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartvpn from '@push.rocks/smartvpn';
import * as smartradius from '@push.rocks/smartradius';
import * as smartrequest from '@push.rocks/smartrequest';
import * as smartrx from '@push.rocks/smartrx';
import * as smartunique from '@push.rocks/smartunique';
import * as taskbuffer from '@push.rocks/taskbuffer';

export { projectinfo, qenv, smartacme, smartdata, smartdns, smartfs, smartguard, smartjwt, smartlog, smartmetrics, smartdb, smartmta, smartnetwork, smartpath, smartproxy, smartpromise, smartradius, smartrequest, smartrx, smartunique, smartvpn, taskbuffer };

// Define SmartLog types for use in error handling
export type TLogLevel = 'error' | 'warn' | 'info' | 'success' | 'debug';

// apiclient.xyz scope
import * as cloudflare from '@apiclient.xyz/cloudflare';

export {
  cloudflare,
}

// tsclass scope
import * as tsclass from '@tsclass/tsclass';

export {
  tsclass,
}

// third party
import * as uuid from 'uuid';

export {
  uuid,
}

// Filesystem utilities
export const fsUtils = {
  /**
   * Ensure a directory exists, creating it recursively if needed (sync)
   */
  ensureDirSync: (dirPath: string): void => {
    fs.mkdirSync(dirPath, { recursive: true });
  },

  /**
   * Ensure a directory exists, creating it recursively if needed (async)
   */
  ensureDir: async (dirPath: string): Promise<void> => {
    await fs.promises.mkdir(dirPath, { recursive: true });
  },

  /**
   * Write JSON content to a file synchronously
   */
  toFsSync: (content: any, filePath: string): void => {
    const data = typeof content === 'string' ? content : JSON.stringify(content, null, 2);
    fs.writeFileSync(filePath, data);
  },

  /**
   * Write JSON content to a file asynchronously
   */
  toFs: async (content: any, filePath: string): Promise<void> => {
    const data = typeof content === 'string' ? content : JSON.stringify(content, null, 2);
    await fs.promises.writeFile(filePath, data);
  },

  /**
   * Check if a file or directory exists
   */
  fileExistsSync: (filePath: string): boolean => {
    return fs.existsSync(filePath);
  },

  /**
   * Check if a file or directory exists (async)
   */
  fileExists: async (filePath: string): Promise<boolean> => {
    try {
      await fs.promises.access(filePath);
      return true;
    } catch {
      return false;
    }
  },

  /**
   * Read a JSON file synchronously
   */
  toObjectSync: <T = any>(filePath: string): T => {
    const content = fs.readFileSync(filePath, 'utf8');
    return JSON.parse(content) as T;
  },

  /**
   * Read a JSON file asynchronously
   */
  toObject: async <T = any>(filePath: string): Promise<T> => {
    const content = await fs.promises.readFile(filePath, 'utf8');
    return JSON.parse(content) as T;
  },
};
