#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

const PROJECT_ROOT = path.resolve(__dirname, '..');
const STANDALONE_EDITOR_DIR = path.join(__dirname, 'standalone-editor');
const ASSETS_DIR = path.join(STANDALONE_EDITOR_DIR, 'assets');
const SRC_DIR = path.join(STANDALONE_EDITOR_DIR, 'src');
const DXPR_BUILDER_DIR = path.join(PROJECT_ROOT, 'dxpr_builder');

function ensureDirectoryExists(dirPath) {
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }
}

function copyDirWithFilters(src, dest) {
  if (!fs.existsSync(src)) {
    ensureDirectoryExists(dest);
    console.log(`Source directory doesn't exist: ${src}`);
    return;
  }

  ensureDirectoryExists(dest);
  const entries = fs.readdirSync(src, { withFileTypes: true });

  for (const entry of entries) {
    const srcPath = path.join(src, entry.name);
    const destPath = path.join(dest, entry.name);

    if (entry.isDirectory() && entry.name === 'node_modules') continue;

    if (entry.isDirectory()) {
      copyDirWithFilters(srcPath, destPath);
    } else {
      if (fs.existsSync(destPath)) {
        const srcStat = fs.statSync(srcPath);
        const destStat = fs.statSync(destPath);
        if (srcStat.size === destStat.size) continue;
      }
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

function copyRootFiles() {
  if (!fs.existsSync(DXPR_BUILDER_DIR)) {
    ensureDirectoryExists(ASSETS_DIR);
    return;
  }
  
  const files = fs.readdirSync(DXPR_BUILDER_DIR, { withFileTypes: true });
  
  for (const file of files) {
    if (file.isFile() && file.name.endsWith('.js')) {
      const srcPath = path.join(DXPR_BUILDER_DIR, file.name);
      const destPath = path.join(ASSETS_DIR, file.name);
      
      if (fs.existsSync(destPath)) {
        const srcStat = fs.statSync(srcPath);
        const destStat = fs.statSync(destPath);
        if (srcStat.size === destStat.size) continue;
      }
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

const directoriesToCopy = [
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/images-backend'),
    dest: path.join(ASSETS_DIR, 'images-backend')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/images-frontend'),
    dest: path.join(ASSETS_DIR, 'images-frontend')
  },
  {
    src: path.join(PROJECT_ROOT, 'js/file-upload'),
    dest: path.join(ASSETS_DIR, 'js/file-upload')
  },
  {
    src: path.join(__dirname, 'standalone-editor/src'),
    dest: path.join(ASSETS_DIR)
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor'),
    dest: path.join(ASSETS_DIR, 'vendor')
  },
  // Add specific vendor managed directories that might be missing
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed'),
    dest: path.join(ASSETS_DIR, 'vendor')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/bootstrap-5'),
    dest: path.join(ASSETS_DIR, 'vendor/bootstrap-5')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/bootstrap-notify'),
    dest: path.join(ASSETS_DIR, 'vendor/bootstrap-notify')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/sortablejs'),
    dest: path.join(ASSETS_DIR, 'vendor/sortablejs')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/draggabilly'),
    dest: path.join(ASSETS_DIR, 'vendor/draggabilly')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/dxb-count-to'),
    dest: path.join(ASSETS_DIR, 'vendor/dxb-count-to')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/lz-string'),
    dest: path.join(ASSETS_DIR, 'vendor/lz-string')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/base64-js'),
    dest: path.join(ASSETS_DIR, 'vendor/base64-js')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/chosen-js'),
    dest: path.join(ASSETS_DIR, 'vendor/chosen-js')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/moveable'),
    dest: path.join(ASSETS_DIR, 'vendor/moveable')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/@floating-ui'),
    dest: path.join(ASSETS_DIR, 'vendor/@floating-ui')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/twig'),
    dest: path.join(ASSETS_DIR, 'vendor/twig')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/underscore'),
    dest: path.join(ASSETS_DIR, 'vendor/underscore')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/@simonwep'),
    dest: path.join(ASSETS_DIR, 'vendor/@simonwep')
  },
  // Ensure pickr directory structure exists in the destination
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/@simonwep/pickr'),
    dest: path.join(ASSETS_DIR, 'vendor/@simonwep/pickr')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/@simonwep/pickr/dist'),
    dest: path.join(ASSETS_DIR, 'vendor/@simonwep/pickr/dist')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/vendor/managed/@simonwep/pickr/dist/themes'),
    dest: path.join(ASSETS_DIR, 'vendor/@simonwep/pickr/dist/themes')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/css'),
    dest: path.join(ASSETS_DIR, 'css')
  },
  {
    src: path.join(PROJECT_ROOT, 'dxpr_builder/js'),
    dest: path.join(ASSETS_DIR, 'js')
  },
  {
    src: path.join(PROJECT_ROOT, 'css'),
    dest: path.join(ASSETS_DIR, 'css')
  },
  {
    src: path.join(PROJECT_ROOT, 'js'),
    dest: path.join(ASSETS_DIR, 'js')
  }
];

ensureDirectoryExists(ASSETS_DIR);

for (const dir of directoriesToCopy) {
  copyDirWithFilters(dir.src, dir.dest);
}

copyRootFiles();
console.log('Asset setup completed.');