/* eslint-disable */
const fs = require('fs');
const path = require('path');

/**
 * GitHub 发布脚本
 *
 * 此脚本用于将 conference/packages/TUIRoomKit-web-atomicx-vue 文件夹下的内容
 * 更新到 ../../../../../TUIRoomKit/Web/roomkit/vue3 文件夹下面
 */

// Target directory - relative to the script location
// This script is in packages/TUIRoomKit-web-atomicx-vue/scripts/
// Target should be: ../../../../../TUIRoomKit/Web/roomkit/vue3
const targetDir = '../../../../../TUIRoomKit/Web/roomkit/vue3';

// Exclude paths that should not be copied (relative to source directory)
const excludePaths = [
  '.vscode',
  '.git',
  'dist',
  'node_modules',
  'scripts',
  '.eslintrc.cjs',
  '.gitignore',
  '.npmignore',
];

/**
 * Copy directory recursively
 * @param {string} fromPath - Source directory path
 * @param {string} toPath - Target directory path
 */
function copyDir(fromPath, toPath, basePath) {
  fs.mkdirSync(toPath, { recursive: true });
  const dirsPath = fs.readdirSync(fromPath);
  dirsPath.forEach((dirPath) => {
    const from = path.join(fromPath, dirPath);
    const to = path.join(toPath, dirPath);

    // Get relative path from base directory
    const relativePath = path.relative(basePath, from);

    // Check if this path should be excluded
    const shouldExclude = excludePaths.some(exclude => {
      return relativePath === exclude || relativePath.startsWith(exclude + path.sep);
    });

    if (shouldExclude) {
      return;
    }

    if (fs.statSync(from).isDirectory()) {
      copyDir(from, to, basePath);
    } else {
      copyFile(from, to);
    }
  });
}

/**
 * Copy file
 * @param {string} fromPath - Source file path
 * @param {string} toPath - Target file path
 */
function copyFile(fromPath, toPath) {
  fs.copyFileSync(fromPath, toPath);
}

/**
 * Remove directory or file recursively
 * @param {string} originPath - Path to remove
 */
function removeDir(originPath) {
  let stats;
  try {
    stats = fs.statSync(originPath);
  } catch (error) {
    return;
  }
  if (stats.isFile()) {
    fs.unlinkSync(originPath);
  } else if (stats.isDirectory()) {
    const dirs = fs.readdirSync(originPath);
    const dirsPath = dirs.map(dir => path.join(originPath, dir));
    dirsPath.forEach((dirPath) => {
      removeDir(dirPath);
    });
    fs.rmdirSync(originPath);
  }
}

// Get absolute paths
const scriptDir = path.resolve(__dirname);
const fromPath = path.resolve(scriptDir, '../'); // Source: packages/TUIRoomKit-web-atomicx-vue
const toPath = path.resolve(scriptDir, targetDir); // Target: TUIRoomKit/Web/roomKit/vue3

console.log('🚀 开始发布到 GitHub...');
console.log('📁 源目录:', fromPath);
console.log('📁 目标目录:', toPath);

// Check if source directory exists
if (!fs.existsSync(fromPath)) {
  console.error('❌ 源目录不存在:', fromPath);
  process.exit(1);
}

// Remove target directory if it exists
if (fs.existsSync(toPath)) {
  console.log('🗑️  删除目标目录...');
  removeDir(toPath);
  console.log('✅ 目标目录已删除');
}

// Copy source content to target directory
console.log('📋 开始复制文件...');
copyDir(fromPath, toPath, fromPath);
console.log('✅ 文件复制完成');

console.log('\n✨ GitHub 发布完成！');
console.log(`📁 发布位置: ${toPath}`);
