// import * as cron from 'node-cron';
const cron:any = {}
import { toggleFlashDeliveryForItems, toggleKeyVal } from '@/src/dbService';
import { CONST_KEYS } from '@/src/lib/const-keys'
import { computeConstants } from '@/src/lib/const-store'


const MUTTON_ITEMS = [
  12, //Lamb mutton
  14, // Mutton Boti
  35, //Mutton Kheema
  84, //Mutton Brain
  4,  //Mutton
  86, //Mutton Chops
  87, //Mutton Soup bones
  85 //Mutton paya
]; 



export const startAutomatedJobs = () => {
  // Job to disable flash delivery for mutton at 12 PM daily
  cron.schedule('0 12 * * *', async () => {
    try {
      console.log('Disabling flash delivery for products at 12 PM');
      await toggleFlashDeliveryForItems(false, MUTTON_ITEMS);
      console.log('Flash delivery disabled successfully');
    } catch (error) {
      console.error('Error disabling flash delivery:', error);
    }
  });

  // Job to enable flash delivery for mutton at 6 AM daily
  cron.schedule('0 6 * * *', async () => {
    try {
      console.log('Enabling flash delivery for products at 5 AM');
      await toggleFlashDeliveryForItems(true, MUTTON_ITEMS);
      console.log('Flash delivery enabled successfully');
     } catch (error) {
      console.error('Error enabling flash delivery:', error);
    }
  });

  // Job to disable flash delivery feature at 9 PM daily
  cron.schedule('0 21 * * *', async () => {
    try {
      console.log('Disabling flash delivery feature at 9 PM');
      await toggleKeyVal(CONST_KEYS.isFlashDeliveryEnabled, false);
      await computeConstants(); // Refresh Redis cache
      console.log('Flash delivery feature disabled successfully');
    } catch (error) {
      console.error('Error disabling flash delivery feature:', error);
    }
  });

  // Job to enable flash delivery feature at 6 AM daily
  cron.schedule('0 6 * * *', async () => {
    try {
      console.log('Enabling flash delivery feature at 6 AM');
      await toggleKeyVal(CONST_KEYS.isFlashDeliveryEnabled, true);
      await computeConstants(); // Refresh Redis cache
      console.log('Flash delivery feature enabled successfully');
    } catch (error) {
      console.error('Error enabling flash delivery feature:', error);
    }
  });

  console.log('Automated jobs scheduled');
};

/*
// Old implementation - direct DB queries:
import { db } from '@/src/db/db_index'
import { productInfo, keyValStore } from '@/src/db/schema'
import { inArray, eq } from 'drizzle-orm';

// Job to disable flash delivery for mutton at 12 PM daily
cron.schedule('0 12 * * *', async () => {
  try {
    console.log('Disabling flash delivery for products at 12 PM');
    await db
      .update(productInfo)
      .set({ isFlashAvailable: false })
      .where(inArray(productInfo.id, MUTTON_ITEMS));
    console.log('Flash delivery disabled successfully');
  } catch (error) {
    console.error('Error disabling flash delivery:', error);
  }
});

// Job to enable flash delivery for mutton at 6 AM daily
cron.schedule('0 6 * * *', async () => {
  try {
    console.log('Enabling flash delivery for products at 5 AM');
    await db
      .update(productInfo)
      .set({ isFlashAvailable: true })
      .where(inArray(productInfo.id, MUTTON_ITEMS));
    console.log('Flash delivery enabled successfully');
   } catch (error) {
    console.error('Error enabling flash delivery:', error);
  }
});

// Job to disable flash delivery feature at 9 PM daily
cron.schedule('0 21 * * *', async () => {
  try {
    console.log('Disabling flash delivery feature at 9 PM');
    await db
      .update(keyValStore)
      .set({ value: false })
      .where(eq(keyValStore.key, CONST_KEYS.isFlashDeliveryEnabled));
    await computeConstants(); // Refresh Redis cache
    console.log('Flash delivery feature disabled successfully');
  } catch (error) {
    console.error('Error disabling flash delivery feature:', error);
  }
});

// Job to enable flash delivery feature at 6 AM daily
cron.schedule('0 6 * * *', async () => {
  try {
    console.log('Enabling flash delivery feature at 6 AM');
    await db
      .update(keyValStore)
      .set({ value: true })
      .where(eq(keyValStore.key, CONST_KEYS.isFlashDeliveryEnabled));
    await computeConstants(); // Refresh Redis cache
    console.log('Flash delivery feature enabled successfully');
  } catch (error) {
    console.error('Error enabling flash delivery feature:', error);
  }
});
*/

// Optional: Call on import if desired, or export and call in main app
// startAutomatedJobs();
