/**
 * Leaderboard Notifications tRPC Router
 * Handles real-time notifications for leaderboard updates and bonus triggers
 */

import { router, protectedProcedure, publicProcedure } from '../_core/trpc.ts';
import { z } from 'zod';
import { leaderboardNotifications } from '../leaderboardNotifications.ts';

export const leaderboardNotificationsRouter = router({
  /**
   * Get all notifications for current user
   */
  getNotifications: protectedProcedure
    .input(
      z.object({
        limit: z.number().int().positive().max(500).default(50),
      })
    )
    .query(({ input, ctx }) => {
      const notifications = leaderboardNotifications.getAllNotifications(ctx.user.id, input.limit);
      return {
        notifications,
        unreadCount: leaderboardNotifications.getUnreadCount(ctx.user.id),
      };
    }),

  /**
   * Get leaderboard notifications only
   */
  getLeaderboardNotifications: protectedProcedure
    .input(
      z.object({
        limit: z.number().int().positive().max(500).default(50),
      })
    )
    .query(({ input, ctx }) => {
      const notifications = leaderboardNotifications.getNotifications(ctx.user.id, input.limit);
      return {
        notifications,
        count: notifications.length,
      };
    }),

  /**
   * Get bonus notifications only
   */
  getBonusNotifications: protectedProcedure
    .input(
      z.object({
        limit: z.number().int().positive().max(500).default(50),
      })
    )
    .query(({ input, ctx }) => {
      const notifications = leaderboardNotifications.getBonusNotifications(ctx.user.id, input.limit);
      return {
        notifications,
        count: notifications.length,
      };
    }),

  /**
   * Get unread notification count
   */
  getUnreadCount: protectedProcedure.query(({ ctx }) => {
    const unreadCount = leaderboardNotifications.getUnreadCount(ctx.user.id);
    return { unreadCount };
  }),

  /**
   * Mark notification as read
   */
  markAsRead: protectedProcedure
    .input(
      z.object({
        notificationId: z.string(),
      })
    )
    .mutation(({ input, ctx }) => {
      leaderboardNotifications.markAsRead(ctx.user.id, input.notificationId);
      return { success: true };
    }),

  /**
   * Mark all notifications as read
   */
  markAllAsRead: protectedProcedure.mutation(({ ctx }) => {
    leaderboardNotifications.markAllAsRead(ctx.user.id);
    return { success: true };
  }),

  /**
   * Clear all notifications
   */
  clearAll: protectedProcedure.mutation(({ ctx }) => {
    leaderboardNotifications.clearNotifications(ctx.user.id);
    return { success: true };
  }),

  /**
   * Subscribe to real-time notifications (WebSocket)
   * This is a placeholder for WebSocket subscription
   * In production, use WebSocket or Server-Sent Events
   */
  subscribeToNotifications: protectedProcedure.subscription(({ ctx }) => {
    // This would be implemented with WebSocket or Server-Sent Events
    // For now, return a placeholder
    return {
      userId: ctx.user.id,
      subscribed: true,
    };
  }),

  /**
   * Trigger test notification (for development)
   */
  triggerTestNotification: protectedProcedure
    .input(
      z.object({
        type: z.enum(['rank_up', 'bonus_trigger', 'milestone']),
      })
    )
    .mutation(({ input, ctx }) => {
      switch (input.type) {
        case 'rank_up':
          leaderboardNotifications.notifyRankUp(ctx.user.id, 'neon-nights', 'weekly', 50, 45);
          break;
        case 'bonus_trigger':
          leaderboardNotifications.notifyBonusTriggered(ctx.user.id, 'wheel-spin', 'neon-nights', 2.5);
          break;
        case 'milestone':
          leaderboardNotifications.notifyMilestone(ctx.user.id, 'neon-nights', '1000 spins completed');
          break;
      }

      return { success: true };
    }),
});
