import { protectedProcedure, publicProcedure, router } from "../_core/trpc.ts";
import { z } from "zod";
import { socialManager } from "../social.ts";

export const socialRouter = router({
  // Get player profile
  getProfile: publicProcedure
    .input(z.object({ playerId: z.number() }))
    .query(async ({ input }) => {
      const profile = await socialManager.getProfile(input.playerId);
      return profile;
    }),

  // Get current user profile
  getMyProfile: protectedProcedure.query(async ({ ctx }) => {
    const profile = await socialManager.getProfile(ctx.user.id);
    return profile;
  }),

  // Update profile
  updateProfile: protectedProcedure
    .input(
      z.object({
        bio: z.string().optional(),
        avatar: z.string().optional(),
      })
    )
    .mutation(async ({ ctx, input }) => {
      const profile = await socialManager.updateProfile(ctx.user.id, input);
      return profile;
    }),

  // Send friend request
  sendFriendRequest: protectedProcedure
    .input(z.object({ targetId: z.number() }))
    .mutation(async ({ ctx, input }) => {
      const result = await socialManager.sendFriendRequest(ctx.user.id, input.targetId);
      return result;
    }),

  // Accept friend request
  acceptFriendRequest: protectedProcedure
    .input(z.object({ requesterId: z.number() }))
    .mutation(async ({ ctx, input }) => {
      const result = await socialManager.acceptFriendRequest(ctx.user.id, input.requesterId);
      return result;
    }),

  // Reject friend request
  rejectFriendRequest: protectedProcedure
    .input(z.object({ requesterId: z.number() }))
    .mutation(async ({ ctx, input }) => {
      const result = await socialManager.rejectFriendRequest(ctx.user.id, input.requesterId);
      return result;
    }),

  // Remove friend
  removeFriend: protectedProcedure
    .input(z.object({ friendId: z.number() }))
    .mutation(async ({ ctx, input }) => {
      const result = await socialManager.removeFriend(ctx.user.id, input.friendId);
      return result;
    }),

  // Get friends list
  getFriendsList: protectedProcedure.query(async ({ ctx }) => {
    const friends = await socialManager.getFriendsList(ctx.user.id);
    return friends;
  }),

  // Get pending friend requests
  getPendingRequests: protectedProcedure.query(async ({ ctx }) => {
    const requests = await socialManager.getPendingRequests(ctx.user.id);
    return requests;
  }),

  // Get leaderboard
  getLeaderboard: publicProcedure
    .input(
      z.object({
        type: z.enum(['total_wagered', 'total_won', 'games_played', 'level']),
        limit: z.number().default(100),
      })
    )
    .query(async ({ input }) => {
      const leaderboard = await socialManager.getLeaderboard(input.type, input.limit);
      return leaderboard;
    }),

  // Get player's leaderboard position
  getPlayerPosition: protectedProcedure
    .input(
      z.object({
        type: z.enum(['total_wagered', 'total_won', 'games_played', 'level']),
      })
    )
    .query(async ({ ctx, input }) => {
      const position = await socialManager.getPlayerPosition(ctx.user.id, input.type);
      return position;
    }),

  // Search players
  searchPlayers: publicProcedure
    .input(z.object({ query: z.string(), limit: z.number().default(20) }))
    .query(async ({ input }) => {
      const players = await socialManager.searchPlayers(input.query, input.limit);
      return players;
    }),

  // Get player activity
  getPlayerActivity: publicProcedure
    .input(z.object({ playerId: z.number() }))
    .query(async ({ input }) => {
      const activity = await socialManager.getPlayerActivity(input.playerId);
      return activity;
    }),

  // Update player stats (called after each game)
  updateStats: protectedProcedure
    .input(
      z.object({
        totalWagered: z.number().optional(),
        totalWon: z.number().optional(),
        gamesPlayed: z.number().optional(),
      })
    )
    .mutation(async ({ ctx, input }) => {
      await socialManager.updatePlayerStats(ctx.user.id, input);
      return { success: true };
    }),

  // Get global stats
  getGlobalStats: publicProcedure.query(async () => {
    // Return aggregate statistics
    return {
      totalPlayers: 0,
      totalWagered: 0,
      totalWon: 0,
      averageWagerPerPlayer: 0,
      topPlayer: null,
    };
  }),

  // Get friends leaderboard (only friends)
  getFriendsLeaderboard: protectedProcedure
    .input(
      z.object({
        type: z.enum(['total_wagered', 'total_won', 'games_played', 'level']),
      })
    )
    .query(async ({ ctx, input }) => {
      const friends = await socialManager.getFriendsList(ctx.user.id);
      const friendIds = friends.map((f) => f.id);

      // Get full leaderboard and filter to friends
      const fullLeaderboard = await socialManager.getLeaderboard(input.type, 10000);
      const friendsLeaderboard = fullLeaderboard.filter((entry) => friendIds.includes(entry.playerId));

      return friendsLeaderboard;
    }),
});
