// Migration script to import content from CSVs to Payload CMS

const fs = require('fs');
const path = require('path');
const csv = require('csv-parser'); // Assume installed
const { Payload } = require('payload');

async function migrate() {
  // Initialize Payload
  const payload = new Payload({
    // Config
  });

  // Read CSV files
  const csvFiles = [
    'research/恩群數位行銷 - 文章分類s.csv',
    'research/恩群數位行銷 - 行銷放大鏡集.csv',
    'research/恩群數位行銷 - 網站設計範本s.csv'
  ];

  for (const file of csvFiles) {
    const data = [];
    fs.createReadStream(path.join(__dirname, '../../', file))
      .pipe(csv())
      .on('data', (row) => data.push(row))
      .on('end', async () => {
        // Import to Payload collections
        for (const item of data) {
          try {
            await payload.create({
              collection: 'posts', // or appropriate collection
              data: item
            });
          } catch (error) {
            console.error('Migration error:', error);
          }
        }
      });
  }
}

migrate().catch(console.error);