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

export const gameApprovalRouter = router({
  // Get all pending approvals
  getPendingApprovals: adminProcedure.query(async ({ ctx }) => {
    // Mock data - replace with actual database query
    return [
      {
        id: '1',
        gameName: 'Golden Fortune',
        provider: 'PGSOFT',
        submittedBy: 'Admin User',
        submittedAt: new Date('2026-04-13T02:00:00Z'),
        status: 'pending',
        rtp: 96.5,
        volatility: 'medium',
      },
      {
        id: '2',
        gameName: 'Mystical Gems',
        provider: 'PGSOFT',
        submittedBy: 'Admin User',
        submittedAt: new Date('2026-04-11T10:00:00Z'),
        status: 'changes_requested',
        rtp: 94.8,
        volatility: 'low',
        feedback: 'Please adjust RTP to 96% and resubmit for review',
      },
    ];
  }),

  // Get approval by ID
  getApprovalById: adminProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ input, ctx }) => {
      // Mock data - replace with actual database query
      return {
        id: input.id,
        gameName: 'Golden Fortune',
        provider: 'PGSOFT',
        submittedBy: 'Admin User',
        submittedAt: new Date('2026-04-13T02:00:00Z'),
        status: 'pending',
        rtp: 96.5,
        volatility: 'medium',
      };
    }),

  // Approve a game
  approveGame: adminProcedure
    .input(z.object({ 
      id: z.string(),
      feedback: z.string().optional(),
    }))
    .mutation(async ({ input, ctx }) => {
      // Update game status in database
      // Send notification to game developer
      // Log approval action
      
      return {
        success: true,
        message: 'Game approved successfully',
        approvedAt: new Date(),
        approvedBy: ctx.user.id,
      };
    }),

  // Reject a game
  rejectGame: adminProcedure
    .input(z.object({ 
      id: z.string(),
      feedback: z.string(),
    }))
    .mutation(async ({ input, ctx }) => {
      // Update game status in database
      // Send notification to game developer with feedback
      // Log rejection action
      
      return {
        success: true,
        message: 'Game rejected',
        rejectedAt: new Date(),
        rejectedBy: ctx.user.id,
        feedback: input.feedback,
      };
    }),

  // Request changes for a game
  requestChanges: adminProcedure
    .input(z.object({ 
      id: z.string(),
      feedback: z.string(),
    }))
    .mutation(async ({ input, ctx }) => {
      // Update game status to changes_requested
      // Send notification to game developer with feedback
      // Log request action
      
      return {
        success: true,
        message: 'Changes requested',
        requestedAt: new Date(),
        requestedBy: ctx.user.id,
        feedback: input.feedback,
      };
    }),

  // Get approval statistics
  getApprovalStats: adminProcedure.query(async ({ ctx }) => {
    // Mock data - replace with actual database aggregation
    return {
      pending: 3,
      approved: 45,
      rejected: 8,
      changesRequested: 2,
      avgApprovalTime: 2.5, // hours
      approvalRate: 84.9, // percentage
    };
  }),

  // Get approval history
  getApprovalHistory: adminProcedure
    .input(z.object({
      limit: z.number().default(20),
      offset: z.number().default(0),
    }))
    .query(async ({ input, ctx }) => {
      // Mock data - replace with actual database query
      return {
        total: 55,
        approvals: [
          {
            id: '1',
            gameName: 'Dragon\'s Treasure',
            status: 'approved',
            approvedAt: new Date('2026-04-13T01:00:00Z'),
            approvedBy: 'Reviewer Name',
          },
          {
            id: '2',
            gameName: 'Lucky Spins',
            status: 'rejected',
            rejectedAt: new Date('2026-04-12T14:00:00Z'),
            rejectedBy: 'Reviewer Name',
            feedback: 'RTP too low',
          },
        ],
      };
    }),
});
