#!/usr/bin/env node

/**
 * CSS Asset Deployment Script
 *
 * Copies generated CSS files from assets/styles/ to public/styles/ for deployment.
 *
 * Files copied:
 * - *.min.css bundles (site, app, enhancements)
 * - website.css (UnoCSS generated)
 * - highlight-github-dark.min.css (syntax highlighting)
 *
 * Usage:
 *   node scripts/copy-css-assets.js
 */

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

async function copyCssAssets() {
  try {
    const assetsStylesDir = path.join(__dirname, '../assets/styles');
    const publicStylesDir = path.join(__dirname, '../public/styles');

    // Ensure public/styles directory exists
    if (!fs.existsSync(publicStylesDir)) {
      fs.mkdirSync(publicStylesDir, { recursive: true });
      console.log('📁 Created public/styles/ directory');
    }

    // Files to copy from assets/styles/ to public/styles/
    const filesToCopy = [
      'site.min.css',
      'app.min.css',
      'enhancements.min.css',
      'website.css',
      'highlight-github-dark.min.css'
    ];

    const copiedFiles = [];
    const missingFiles = [];

    for (const fileName of filesToCopy) {
      const sourcePath = path.join(assetsStylesDir, fileName);
      const destPath = path.join(publicStylesDir, fileName);

      if (fs.existsSync(sourcePath)) {
        fs.copyFileSync(sourcePath, destPath);
        const size = Math.round(fs.statSync(destPath).size / 1024);
        copiedFiles.push(`${fileName} (${size}KB)`);
      } else {
        missingFiles.push(fileName);
      }
    }

    console.log('📦 CSS Asset Deployment Complete!');
    console.log('');

    if (copiedFiles.length > 0) {
      console.log('✅ Copied to public/styles/:');
      copiedFiles.forEach(file => console.log(`   📄 ${file}`));
    }

    if (missingFiles.length > 0) {
      console.log('');
      console.log('⚠️  Missing source files (skipped):');
      missingFiles.forEach(file => console.log(`   ❌ ${file}`));
      console.log('');
      console.log('💡 Run build scripts first to generate missing files');
    }

    const totalFiles = copiedFiles.length;
    const totalSize = copiedFiles.reduce((sum, file) => {
      const sizeMatch = file.match(/\((\d+)KB\)/);
      return sum + (sizeMatch ? parseInt(sizeMatch[1]) : 0);
    }, 0);

    console.log('');
    console.log(`📊 Deployment Summary: ${totalFiles} files, ${totalSize}KB total`);
    console.log('🚀 Ready for Leptos deployment to target/site/');

  } catch (error) {
    console.error('❌ Error copying CSS assets:', error.message);
    process.exit(1);
  }
}

copyCssAssets();
