import { getDb } from "../db.ts";

export type ActivityType = "win" | "achievement" | "tournament" | "milestone" | "referral";

export interface GameWinActivity {
  id: number;
  playerId: number;
  playerName: string;
  playerAvatar?: string;
  gameId: string;
  gameName: string;
  winAmount: number;
  multiplier: number;
  timestamp: Date;
  type: "win";
}

export interface AchievementActivity {
  id: number;
  playerId: number;
  playerName: string;
  playerAvatar?: string;
  achievementId: string;
  achievementName: string;
  achievementIcon: string;
  timestamp: Date;
  type: "achievement";
}

export interface TournamentActivity {
  id: number;
  playerId: number;
  playerName: string;
  playerAvatar?: string;
  tournamentId: string;
  tournamentName: string;
  placement: number;
  prizeAmount: number;
  timestamp: Date;
  type: "tournament";
}

export interface MilestoneActivity {
  id: number;
  playerId: number;
  playerName: string;
  playerAvatar?: string;
  milestoneName: string;
  milestoneIcon: string;
  description: string;
  timestamp: Date;
  type: "milestone";
}

export type Activity = GameWinActivity | AchievementActivity | TournamentActivity | MilestoneActivity;

// In-memory activity feed (in production, use database)
const activityFeed: Activity[] = [];
const MAX_FEED_SIZE = 100;

export async function recordGameWin(
  playerId: number,
  playerName: string,
  playerAvatar: string | undefined,
  gameId: string,
  gameName: string,
  winAmount: number,
  multiplier: number
): Promise<GameWinActivity> {
  const activity: GameWinActivity = {
    id: Date.now(),
    playerId,
    playerName,
    playerAvatar,
    gameId,
    gameName,
    winAmount,
    multiplier,
    timestamp: new Date(),
    type: "win",
  };

  activityFeed.unshift(activity);
  if (activityFeed.length > MAX_FEED_SIZE) {
    activityFeed.pop();
  }

  console.log(`[ACTIVITY] ${playerName} won ${winAmount} GC on ${gameName}`);
  return activity;
}

export async function recordAchievement(
  playerId: number,
  playerName: string,
  playerAvatar: string | undefined,
  achievementId: string,
  achievementName: string,
  achievementIcon: string
): Promise<AchievementActivity> {
  const activity: AchievementActivity = {
    id: Date.now(),
    playerId,
    playerName,
    playerAvatar,
    achievementId,
    achievementName,
    achievementIcon,
    timestamp: new Date(),
    type: "achievement",
  };

  activityFeed.unshift(activity);
  if (activityFeed.length > MAX_FEED_SIZE) {
    activityFeed.pop();
  }

  console.log(`[ACTIVITY] ${playerName} unlocked achievement: ${achievementName}`);
  return activity;
}

export async function recordTournamentWin(
  playerId: number,
  playerName: string,
  playerAvatar: string | undefined,
  tournamentId: string,
  tournamentName: string,
  placement: number,
  prizeAmount: number
): Promise<TournamentActivity> {
  const activity: TournamentActivity = {
    id: Date.now(),
    playerId,
    playerName,
    playerAvatar,
    tournamentId,
    tournamentName,
    placement,
    prizeAmount,
    timestamp: new Date(),
    type: "tournament",
  };

  activityFeed.unshift(activity);
  if (activityFeed.length > MAX_FEED_SIZE) {
    activityFeed.pop();
  }

  console.log(`[ACTIVITY] ${playerName} placed #${placement} in ${tournamentName}`);
  return activity;
}

export async function recordMilestone(
  playerId: number,
  playerName: string,
  playerAvatar: string | undefined,
  milestoneName: string,
  milestoneIcon: string,
  description: string
): Promise<MilestoneActivity> {
  const activity: MilestoneActivity = {
    id: Date.now(),
    playerId,
    playerName,
    playerAvatar,
    milestoneName,
    milestoneIcon,
    description,
    timestamp: new Date(),
    type: "milestone",
  };

  activityFeed.unshift(activity);
  if (activityFeed.length > MAX_FEED_SIZE) {
    activityFeed.pop();
  }

  console.log(`[ACTIVITY] ${playerName} reached milestone: ${milestoneName}`);
  return activity;
}

export async function getActivityFeed(limit: number = 50): Promise<Activity[]> {
  return activityFeed.slice(0, limit);
}

export async function getPlayerActivity(playerId: number, limit: number = 20): Promise<Activity[]> {
  return activityFeed.filter((a) => a.playerId === playerId).slice(0, limit);
}

export async function getActivityByType(type: ActivityType, limit: number = 50): Promise<Activity[]> {
  return activityFeed.filter((a) => a.type === type).slice(0, limit);
}

export async function getHighValueWins(minAmount: number = 1000, limit: number = 20): Promise<GameWinActivity[]> {
  return (activityFeed.filter((a) => a.type === "win" && a.winAmount >= minAmount) as GameWinActivity[]).slice(0, limit);
}

export async function clearActivityFeed(): Promise<void> {
  activityFeed.length = 0;
  console.log("[ACTIVITY] Feed cleared");
}

export async function getActivityStats(): Promise<{
  totalActivities: number;
  totalWins: number;
  totalAchievements: number;
  totalTournaments: number;
  totalMilestones: number;
  totalWinAmount: number;
}> {
  const wins = activityFeed.filter((a) => a.type === "win") as GameWinActivity[];
  const achievements = activityFeed.filter((a) => a.type === "achievement");
  const tournaments = activityFeed.filter((a) => a.type === "tournament");
  const milestones = activityFeed.filter((a) => a.type === "milestone");

  return {
    totalActivities: activityFeed.length,
    totalWins: wins.length,
    totalAchievements: achievements.length,
    totalTournaments: tournaments.length,
    totalMilestones: milestones.length,
    totalWinAmount: wins.reduce((sum, w) => sum + w.winAmount, 0),
  };
}
