import { getPayload } from 'payload';
import configPromise from '../payload.config';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

async function run() {
  process.env.PAYLOAD_MIGRATE = 'false';
  process.env.PAYLOAD_MIGRATE_SKIP_PROMPTS = 'true';
  const payload = await getPayload({ config: configPromise });

  console.log('📖 Reading lexical_avb.json...');
  const lexicalContent = JSON.parse(fs.readFileSync(path.resolve(dirname, '../lexical_avb.json'), 'utf8'));

  console.log('🚀 Updating AGB page (ID 6) to AVB...');
  
  const updatedPage = await payload.update({
    collection: 'pages',
    id: 6,
    data: {
      title: 'Allgemeine Verkaufsbedingungen (AVB)',
      content: lexicalContent,
    },
  });

  console.log('✅ Page updated successfully:', updatedPage.title);
  process.exit(0);
}

run().catch((err) => {
  console.error('❌ Update failed:', err);
  process.exit(1);
});
