import { router, protectedProcedure } from '../_core/trpc.js.ts';
import { z } from 'zod';
import NotificationAnalyticsService from '../notificationAnalyticsService.js';

const analyticsService = new NotificationAnalyticsService();

export const notificationAnalyticsRouter = router({
  /**
   * Track notification event
   */
  trackEvent: protectedProcedure
    .input(
      z.object({
        notificationId: z.string(),
        eventType: z.enum(['sent', 'delivered', 'opened', 'clicked', 'dismissed']),
        channel: z.enum(['push', 'email', 'in_app']),
        metadata: z.record(z.any()).optional(),
      })
    )
    .mutation(({ input, ctx }) => {
      return analyticsService.trackEvent(
        input.notificationId,
        ctx.user.id,
        input.eventType,
        input.channel,
        input.metadata
      );
    }),

  /**
   * Get metrics for a notification
   */
  getMetrics: protectedProcedure
    .input(z.object({ notificationId: z.string() }))
    .query(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return analyticsService.getMetrics(input.notificationId);
    }),

  /**
   * Get all metrics (admin only)
   */
  getAllMetrics: protectedProcedure.query(({ ctx }) => {
    // Check if user is admin
    if (ctx.user.role !== 'admin') {
      throw new Error('Unauthorized');
    }

    return analyticsService.getAllMetrics();
  }),

  /**
   * Get events for a notification
   */
  getNotificationEvents: protectedProcedure
    .input(z.object({ notificationId: z.string() }))
    .query(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return analyticsService.getNotificationEvents(input.notificationId);
    }),

  /**
   * Get user's events
   */
  getUserEvents: protectedProcedure
    .input(z.object({ limit: z.number().default(100) }))
    .query(({ input, ctx }) => {
      return analyticsService.getUserEvents(ctx.user.id, input.limit);
    }),

  /**
   * Get analytics summary (admin only)
   */
  getAnalyticsSummary: protectedProcedure
    .input(
      z.object({
        startDate: z.date().optional(),
        endDate: z.date().optional(),
      })
    )
    .query(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return analyticsService.getAnalyticsSummary(input.startDate, input.endDate);
    }),

  /**
   * Get top performing notifications (admin only)
   */
  getTopPerformingNotifications: protectedProcedure
    .input(z.object({ limit: z.number().default(10) }))
    .query(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return analyticsService.getTopPerformingNotifications(input.limit);
    }),

  /**
   * Get channel performance (admin only)
   */
  getChannelPerformance: protectedProcedure.query(({ ctx }) => {
    // Check if user is admin
    if (ctx.user.role !== 'admin') {
      throw new Error('Unauthorized');
    }

    return analyticsService.getChannelPerformance();
  }),
});

export default notificationAnalyticsRouter;
