module.exports = {
  checkPublish: {
    task: async ({ strapi }) => {
      const now = new Date();
      const drafts = await strapi.documents('api::blog.blog').findMany({
        filters: { 
          publish_at: { $lte: now },
          isNewVersion: true 
        },
        status: 'draft',
      });
      for (const draft of drafts) {
        console.log('Publishing entry:', draft.documentId);
        await strapi.documents('api::blog.blog').update({
          documentId: draft.documentId,
          data: { isNewVersion: false },
          status: 'draft',
        });
        await strapi.documents('api::blog.blog').publish({
          documentId: draft.documentId,
        });
      }
    },
    options: {
      rule: '* * * * *',
    },
  },
};