import * as plugins from './plugins.js';

// Base directories
export const baseDir = process.cwd();
export const packageDir = plugins.path.join(
  plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
  '../'
);
export const distServe = plugins.path.join(packageDir, './dist_serve');

// Configure data directory with environment variable or default to .nogit/data
const DEFAULT_DATA_PATH = '.nogit/data';
export const dataDir = process.env.DATA_DIR 
  ? process.env.DATA_DIR 
  : plugins.path.join(baseDir, DEFAULT_DATA_PATH);

// MTA directories 
export const keysDir = plugins.path.join(dataDir, 'keys');
export const dnsRecordsDir = plugins.path.join(dataDir, 'dns');
export const sentEmailsDir = plugins.path.join(dataDir, 'emails', 'sent');
export const receivedEmailsDir = plugins.path.join(dataDir, 'emails', 'received');
export const failedEmailsDir = plugins.path.join(dataDir, 'emails', 'failed'); // For failed emails
export const logsDir = plugins.path.join(dataDir, 'logs'); // For logs

// Email template directories
export const emailTemplatesDir = plugins.path.join(dataDir, 'templates', 'email');
export const MtaAttachmentsDir = plugins.path.join(dataDir, 'attachments'); // For email attachments

// Configuration path
export const configPath = process.env.CONFIG_PATH 
  ? process.env.CONFIG_PATH 
  : plugins.path.join(baseDir, 'config.json');

// Create directories if they don't exist
export async function ensureDirectories() {
  // Ensure data directories
  await plugins.smartfs.directory(dataDir).recursive().create();
  await plugins.smartfs.directory(keysDir).recursive().create();
  await plugins.smartfs.directory(dnsRecordsDir).recursive().create();
  await plugins.smartfs.directory(sentEmailsDir).recursive().create();
  await plugins.smartfs.directory(receivedEmailsDir).recursive().create();
  await plugins.smartfs.directory(failedEmailsDir).recursive().create();
  await plugins.smartfs.directory(logsDir).recursive().create();

  // Ensure email template directories
  await plugins.smartfs.directory(emailTemplatesDir).recursive().create();
  await plugins.smartfs.directory(MtaAttachmentsDir).recursive().create();
}