const eventBus = require('./eventBus');

// 3 & 5-TOPshiriqlar: Yangi kitobga ishlov berish va xatolikni boshqarish
eventBus.on('NEW_BOOK_ADDED', (book) => {
    try {
        if (!book.title) throw new Error("Kitob sarlavhasi yo'q!"); 
        
        console.log('-------------------------------------------');
        console.log('[EVENT SUCCESS]: Yangi kitob xabarnomasi!');
        console.log(`Kitob nomi: ${book.title}`);
        console.log(`Vaqti: ${new Date().toLocaleString()}`); // 8-topshiriq: Monitoring
        console.log('-------------------------------------------');
    } catch (error) {
        console.error(`[EVENT ERROR]: Xatolik yuz berdi: ${error.message}`);
    }
});

// 6-TOPshiriq: O'chirish voqeasini eshitish
eventBus.on('BOOK_DELETED', (data) => {
    console.log('-------------------------------------------');
    console.log(`[EVENT]: Kitob o'chirildi! ID: ${data.id}`);
    console.log('-------------------------------------------');
});