import type { GameConfig } from "@/lib/validations/game";
import type { Player } from "@/server/db/schema";

export type GamePlayer = Pick<Player, "id" | "displayName" | "avatar"> & {
  score: number;
};

export interface IMinigame {
  id: string;
  name: string;
  description: string;
  thumbnail: string;
  maxPlayers: number;
  minPlayers: number;
}

export interface IGameState {
  status: "waiting" | "active" | "paused" | "completed";
  players: Array<GamePlayer>;
  currentRound: number;
  totalRounds: number;
  timeRemaining?: number;
}

// Base Game abstract class
export abstract class Game {
  protected meta: IMinigame;
  protected players: Array<GamePlayer> = [];
  protected config: GameConfig;
  protected state: IGameState;

  constructor(meta: IMinigame, config: GameConfig) {
    this.meta = meta;
    this.config = config;
    this.state = {
      status: "waiting",
      players: [],
      currentRound: 0,
      totalRounds: 0,
    };
  }

  public getGameId(): string {
    return this.meta.id;
  }

  public getState(): IGameState {
    return { ...this.state };
  }

  public addPlayer(player: GamePlayer): void {
    this.players.push(player);
    this.state.players = [...this.players];
  }

  public removePlayer(playerId: string): void {
    this.players = this.players.filter((p) => p.id !== playerId);
    this.state.players = [...this.players];
  }

  abstract initialize(config?: GameConfig): void;
  abstract start(): void;
  abstract end(): void;
  abstract handlePlayerAction(playerId: string, action: any): void;
}
