// app/lib/notificationService.ts
import { bot } from '../telegram/lib/config';
import { eventBus, EVENTS } from './eventBus';
import { UserProduct, UserTicket } from '@/lib/generated/prisma';


class NotificationService {
  constructor() {
    this.init();
  }

  init() {
    eventBus.on(EVENTS.TICKET_CREATED, this.handleTicketCreated.bind(this));
    eventBus.on(EVENTS.PRODUCT_BOUGHT, this.handleProductBought.bind(this));
  }
  async handleTicketCreated(ticket: UserTicket) {
    const baseUrl = process.env.WEBHOOK_URL?.split('/api/telegram')[0] || 'http://google.com';
    bot.telegram.sendMessage(
      -4844856594,
      `Ticket Created: ${ticket.content}`,
      {
        parse_mode: 'HTML',
        reply_markup: {
          inline_keyboard: [
            [
              {
                text: "View Tickets",
                url: `${baseUrl}/dashboard/users/ticket`
              }
            ]
          ]
        }
      }
    );
  }
  async handleProductBought(userProduct: UserProduct) {
    const baseUrl = process.env.WEBHOOK_URL?.split('/api/telegram')[0] || 'http://google.com';
    bot.telegram.sendMessage(
      -4844856594,
      `Product Bought: ${userProduct.userId}`,
      {
        parse_mode: 'HTML',
        reply_markup: {
          inline_keyboard: [
            [
              {
                text: "View Products",
                url: `${baseUrl}/dashboard/shop/user-products`
              }
            ]
          ]
        }
      }
    );
  }
}

export const notificationService = new NotificationService();

export const initNotifications = () => {
  return notificationService;
};