import { getPayload } from 'payload';
import configPromise from '@payload-config';

async function checkData() {
  try {
    const payload = await getPayload({ config: configPromise });

    const { docs: posts } = await payload.find({ collection: 'posts', limit: 3 });
    const { docs: products } = await payload.find({ collection: 'products', limit: 3 });
    const { docs: pages } = await payload.find({ collection: 'pages', limit: 3 });

    const checkDocs = (name: string, docs: any[]) => {
      console.log(`\n----- ${name.toUpperCase()} -----`);
      docs.forEach((p) => {
        console.log(`ID: ${p.id}, Slug: ${p.slug}`);
        if (Array.isArray(p.content)) {
          console.log(
            'Content is ARRAY (Slate format!)',
            JSON.stringify(p.content).substring(0, 100),
          );
        } else if (p.content && p.content.root) {
          console.log('Content is Lexical format.');
        } else {
          console.log('Content is UNKNOWN format.');
          console.log(JSON.stringify(p.content).substring(0, 100));
        }
      });
    };

    checkDocs('posts', posts);
    checkDocs('products', products);
    checkDocs('pages', pages);
  } catch (err) {
    console.error(err);
  }
  process.exit(0);
}

checkData();
