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

const libraryService = new GameTemplateLibraryService();

export const gameTemplateLibraryRouter = router({
  /**
   * Get template
   */
  getTemplate: protectedProcedure
    .input(z.object({ templateId: z.string() }))
    .query(({ input }) => {
      return libraryService.getTemplate(input.templateId);
    }),

  /**
   * Get all templates
   */
  getAllTemplates: protectedProcedure.query(() => {
    return libraryService.getAllTemplates();
  }),

  /**
   * Get templates by genre
   */
  getTemplatesByGenre: protectedProcedure
    .input(z.object({ genre: z.string() }))
    .query(({ input }) => {
      return libraryService.getTemplatesByGenre(input.genre);
    }),

  /**
   * Create template variant
   */
  createVariant: protectedProcedure
    .input(
      z.object({
        templateId: z.string(),
        name: z.string(),
        description: z.string(),
        config: z.record(z.any()),
        theme: z.record(z.any()),
      })
    )
    .mutation(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return libraryService.createVariant(
        input.templateId,
        input.name,
        input.description,
        input.config,
        input.theme
      );
    }),

  /**
   * Get template variants
   */
  getTemplateVariants: protectedProcedure
    .input(z.object({ templateId: z.string() }))
    .query(({ input }) => {
      return libraryService.getTemplateVariants(input.templateId);
    }),

  /**
   * Get variant
   */
  getVariant: protectedProcedure
    .input(z.object({ variantId: z.string() }))
    .query(({ input }) => {
      return libraryService.getVariant(input.variantId);
    }),

  /**
   * Update template performance
   */
  updateTemplatePerformance: protectedProcedure
    .input(
      z.object({
        templateId: z.string(),
        metrics: z.object({
          gamesCreated: z.number().optional(),
          plays: z.number().optional(),
          wagers: z.number().optional(),
          payouts: z.number().optional(),
          engagementScore: z.number().optional(),
          retention: z.number().optional(),
          revenue: z.number().optional(),
        }),
      })
    )
    .mutation(({ input, ctx }) => {
      // Check if user is admin
      if (ctx.user.role !== 'admin') {
        throw new Error('Unauthorized');
      }

      return libraryService.updateTemplatePerformance(input.templateId, input.metrics);
    }),

  /**
   * Get template performance
   */
  getTemplatePerformance: protectedProcedure
    .input(z.object({ templateId: z.string() }))
    .query(({ input }) => {
      return libraryService.getTemplatePerformance(input.templateId);
    }),

  /**
   * Get top templates
   */
  getTopTemplates: protectedProcedure
    .input(z.object({ limit: z.number().default(10) }))
    .query(({ input }) => {
      return libraryService.getTopTemplates(input.limit);
    }),

  /**
   * Get trending templates
   */
  getTrendingTemplates: protectedProcedure
    .input(z.object({ limit: z.number().default(5) }))
    .query(({ input }) => {
      return libraryService.getTrendingTemplates(input.limit);
    }),

  /**
   * Update template rating
   */
  updateTemplateRating: protectedProcedure
    .input(z.object({ templateId: z.string(), rating: z.number(), review: z.string() }))
    .mutation(({ input }) => {
      return libraryService.updateTemplateRating(input.templateId, input.rating, input.review);
    }),

  /**
   * Increment template downloads
   */
  incrementTemplateDownloads: protectedProcedure
    .input(z.object({ templateId: z.string() }))
    .mutation(({ input }) => {
      return libraryService.incrementTemplateDownloads(input.templateId);
    }),

  /**
   * Search templates
   */
  searchTemplates: protectedProcedure
    .input(z.object({ query: z.string() }))
    .query(({ input }) => {
      return libraryService.searchTemplates(input.query);
    }),

  /**
   * Get template statistics
   */
  getTemplateStatistics: protectedProcedure
    .input(z.object({ templateId: z.string() }))
    .query(({ input }) => {
      return libraryService.getTemplateStatistics(input.templateId);
    }),

  /**
   * Get all template performance
   */
  getAllTemplatePerformance: protectedProcedure.query(() => {
    return libraryService.getAllTemplatePerformance();
  }),

  /**
   * Get best performing templates
   */
  getBestPerformingTemplates: protectedProcedure
    .input(z.object({ limit: z.number().default(5) }))
    .query(({ input }) => {
      return libraryService.getBestPerformingTemplates(input.limit);
    }),
});

export default gameTemplateLibraryRouter;
