import { router, protectedProcedure } from "../_core/trpc.ts";
import { z } from "zod";
import { TRPCError } from "@trpc/server";

export const adminGamesRouter = router({
  // Get all games with optional filtering
  getGames: protectedProcedure
    .input(
      z.object({
        limit: z.number().default(100),
        offset: z.number().default(0),
        provider: z.string().optional(),
        search: z.string().optional(),
      })
    )
    .query(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, query database with filters
      return {
        games: [],
        total: 0,
      };
    }),

  // Get single game details
  getGameDetails: protectedProcedure
    .input(z.object({ gameId: z.string() }))
    .query(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, query database
      return {
        gameId: input.gameId,
        gameName: "",
        provider: "",
        rtp: 96,
        volatility: "medium",
        maxWin: 10000,
        minBet: 0.1,
        maxBet: 100,
        isActive: true,
        isFeatured: false,
        category: "slots",
        thumbnail_url: "",
      };
    }),

  // Update game settings
  updateGameSettings: protectedProcedure
    .input(
      z.object({
        gameId: z.string(),
        rtp: z.number().min(1).max(100).optional(),
        volatility: z.enum(["low", "medium", "high"]).optional(),
        maxWin: z.number().positive().optional(),
        minBet: z.number().positive().optional(),
        maxBet: z.number().positive().optional(),
        category: z.string().optional(),
      })
    )
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, update database
      return {
        success: true,
        message: `Game ${input.gameId} updated successfully`,
      };
    }),

  // Toggle game active status
  toggleGameActive: protectedProcedure
    .input(z.object({ gameId: z.string() }))
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, update database
      return {
        success: true,
        message: `Game ${input.gameId} status toggled`,
      };
    }),

  // Toggle game featured status
  toggleGameFeatured: protectedProcedure
    .input(z.object({ gameId: z.string() }))
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, update database
      return {
        success: true,
        message: `Game ${input.gameId} featured status toggled`,
      };
    }),

  // Create new game
  createGame: protectedProcedure
    .input(
      z.object({
        gameName: z.string().min(1),
        provider: z.string().min(1),
        rtp: z.number().min(1).max(100),
        volatility: z.enum(["low", "medium", "high"]),
        maxWin: z.number().positive(),
        minBet: z.number().positive(),
        maxBet: z.number().positive(),
        category: z.string(),
        thumbnail_url: z.string().url().optional(),
      })
    )
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, insert into database
      return {
        success: true,
        gameId: `game-${Date.now()}`,
        message: "Game created successfully",
      };
    }),

  // Delete game
  deleteGame: protectedProcedure
    .input(z.object({ gameId: z.string() }))
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, delete from database
      return {
        success: true,
        message: `Game ${input.gameId} deleted successfully`,
      };
    }),

  // Bulk update games
  bulkUpdateGames: protectedProcedure
    .input(
      z.object({
        gameIds: z.array(z.string()),
        updates: z.object({
          isActive: z.boolean().optional(),
          isFeatured: z.boolean().optional(),
          category: z.string().optional(),
        }),
      })
    )
    .mutation(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, update multiple games in database
      return {
        success: true,
        updated: input.gameIds.length,
        message: `${input.gameIds.length} games updated successfully`,
      };
    }),

  // Get game statistics
  getGameStats: protectedProcedure
    .input(z.object({ gameId: z.string() }))
    .query(async ({ ctx, input }) => {
      if (ctx.user.role !== "admin") {
        throw new TRPCError({ code: "FORBIDDEN" });
      }

      // In production, query game statistics from database
      return {
        gameId: input.gameId,
        totalPlays: 0,
        totalRevenue: 0,
        totalWinnings: 0,
        averageBet: 0,
        averageSessionDuration: 0,
        playerRetention: 0,
        lastUpdated: new Date(),
      };
    }),
});
