// 简单的构建脚本
const fs = require('fs');
const path = require('path');

console.log('开始简单构建飞鹅打印模块...');

const srcDir = path.join(__dirname, 'src');
const distDir = path.join(__dirname, 'dist');

// 确保dist目录存在
if (!fs.existsSync(distDir)) {
  fs.mkdirSync(distDir, { recursive: true });
}

// 复制所有.js文件
function copyJsFiles(dir, relativePath = '') {
  const files = fs.readdirSync(dir);

  for (const file of files) {
    const fullPath = path.join(dir, file);
    const stat = fs.statSync(fullPath);

    if (stat.isDirectory()) {
      copyJsFiles(fullPath, path.join(relativePath, file));
    } else if (file.endsWith('.js')) {
      const destDir = path.join(distDir, relativePath);
      if (!fs.existsSync(destDir)) {
        fs.mkdirSync(destDir, { recursive: true });
      }

      const destPath = path.join(destDir, file);
      fs.copyFileSync(fullPath, destPath);
      console.log(`复制: ${path.join(relativePath, file)}`);
    } else if (file.endsWith('.ts')) {
      // 对于.ts文件，我们创建一个简单的.js版本
      const jsFile = file.replace('.ts', '.js');
      const destDir = path.join(distDir, relativePath);
      if (!fs.existsSync(destDir)) {
        fs.mkdirSync(destDir, { recursive: true });
      }

      const destPath = path.join(destDir, jsFile);
      // 创建一个简单的导出文件
      const moduleName = path.basename(file, '.ts');
      const content = `// 自动生成的简单版本
console.warn('使用自动生成的简单版本: ${moduleName}');
module.exports = {};`;

      fs.writeFileSync(destPath, content);
      console.log(`创建简单版本: ${path.join(relativePath, jsFile)}`);
    }
  }
}

// 创建index.js
const indexPath = path.join(distDir, 'index.js');
const indexContent = `// 自动生成的index.js
console.warn('使用自动生成的飞鹅打印模块简单版本');

// 导出主要服务
exports.PrintTriggerService = require('./services/print-trigger.service.js').PrintTriggerService;
exports.PrintTaskService = require('./services/print-task.service.js').PrintTaskService;
exports.PrinterService = require('./services/printer.service.js').PrinterService;
exports.DelaySchedulerService = require('./services/delay-scheduler.service.js').DelaySchedulerService;
exports.FeieApiService = require('./services/feie-api.service.js').FeieApiService;

// 导出类型
exports.PrintType = { RECEIPT: 'RECEIPT', SHIPPING: 'SHIPPING', ORDER: 'ORDER' };
exports.PrintStatus = {
  PENDING: 'PENDING',
  DELAYED: 'DELAYED',
  PRINTING: 'PRINTING',
  SUCCESS: 'SUCCESS',
  FAILED: 'FAILED',
  CANCELLED: 'CANCELLED'
};
exports.CancelReason = {
  REFUND: 'REFUND',
  MANUAL: 'MANUAL',
  TIMEOUT: 'TIMEOUT'
};

console.log('飞鹅打印模块简单版本已加载');`;

fs.writeFileSync(indexPath, indexContent);
console.log('创建index.js');

// 复制源文件
if (fs.existsSync(srcDir)) {
  copyJsFiles(srcDir);
} else {
  console.log('src目录不存在，创建示例文件...');

  // 创建必要的目录结构
  const servicesDir = path.join(distDir, 'services');
  fs.mkdirSync(servicesDir, { recursive: true });

  // 创建简单的PrintTriggerService
  const printTriggerServicePath = path.join(servicesDir, 'print-trigger.service.js');
  const printTriggerServiceContent = `class PrintTriggerService {
  constructor(dataSource, feieConfig) {
    console.log('PrintTriggerService created');
    this.dataSource = dataSource;
    this.feieConfig = feieConfig;
  }

  async handleOrderPaymentSuccess(tenantId, orderId) {
    console.log(\`[租户\${tenantId}] 处理订单支付成功事件，订单ID: \${orderId}\`);
    console.log('注意：这是简单版本，不会实际创建打印任务');
    return Promise.resolve();
  }

  async handleOrderRefund(tenantId, orderId) {
    console.log(\`[租户\${tenantId}] 处理订单退款事件，订单ID: \${orderId}\`);
    return Promise.resolve();
  }
}

module.exports = { PrintTriggerService };`;

  fs.writeFileSync(printTriggerServicePath, printTriggerServiceContent);
  console.log('创建简单的PrintTriggerService');
}

console.log('简单构建完成！');