export type ResourceKey = "wood" | "brick" | "sheep" | "wheat" | "stone";

export type ResourceMap = Partial<Record<ResourceKey, number>>;

export interface TransientGameState {
  turnTimer?: any;
  unselected?: any[];
}

export interface Player {
  name: string;
  color: PlayerColor;
  order: number;
  orderRoll: number;
  position: string;
  orderStatus: string;
  tied: boolean;
  roads: number;
  settlements: number;
  cities: number;
  longestRoad: number;
  mustDiscard?: number;
  /* Resources */
  sheep: number;
  wheat: number;
  stone: number;
  brick: number;
  wood: number;
  desert: number; /* Not used -- for Typescript compliance */
  bank: number; /* Not used -- for Typescript compliance */
  /* End Resources */
  army: number;
  points: number;
  ports: number;
  resources: number;
  lastActive: number;
  live: boolean;
  status: string;
  developmentCards: number;
  development: DevelopmentCard[];
  turnNotice: string;
  turnStart: number;
  totalTime: number;
  banks: ResourceType[];
  /* Offer */
  gives: OfferItem[];
  gets: OfferItem[];
  offerRejected: Record<PlayerColor, boolean>;
  /* End Offer */
}

export type CornerType = "settlement" | "city" | "none";
export const CORNER_TYPES: CornerType[] = ["settlement", "city", "none"];

export interface CornerPlacement {
  color: PlayerColor;
  type: "settlement" | "city" | "none";
  walking?: boolean;
  longestRoad?: number;
}

export type RoadType = "road" | "ship";
export const ROAD_TYPES: RoadType[] = ["road", "ship"];

export interface RoadPlacement {
  color?: PlayerColor;
  walking?: boolean;
  type?: "road" | "ship";
  longestRoad?: number;
}

export interface Placements {
  corners: Array<CornerPlacement | undefined>;
  roads: Array<RoadPlacement | undefined>;
}

export interface Turn {
  name?: string;
  color: PlayerColor;
  actions?: string[];
  limits?: any;
  roll?: number;
  volcano?: number | null | undefined;
  free: boolean;
  freeRoads: number;
  select?: Record<string, number>;
  active: "volcano" | "robber" | "road-building" | "offer" | null;
  robberInAction: boolean;
  placedRobber: boolean;
  developmentPurchased: boolean;
  offer: Offer | null;
  [key: string]: any;
}

export interface DevelopmentCard {
  card?: number | string;
  type?: string;
  turn?: number;
}

// Import from schema for DRY compliance
import { TransientSessionState } from "./transientSchema";

/**
 * Persistent Session data (saved to DB)
 */
export interface PersistentSessionData {
  id: string;
  name: string;
  color: PlayerColor;
  lastActive: number;
  userId?: number;
  player?: Player;
  connected?: boolean;
  resources?: number;
}

export type PlayerColor = "R" | "B" | "O" | "W" | "robber" | "bank" | "unassigned";
export const PLAYER_COLORS: PlayerColor[] = ["R", "B", "O", "W", "robber", "bank", "unassigned"];

/**
 * Runtime Session type = Persistent + Transient
 * At runtime, sessions have both persistent and transient fields
 */
export type Session = PersistentSessionData & TransientSessionState;

export interface OfferItem {
  type: ResourceType; // 'bank' or resource key or other
  count: number;
}

export interface Offer {
  gets: OfferItem[];
  gives: OfferItem[];
  name: string;
  color: PlayerColor; // Omit for bank trades
}

export type ResourceType = "wood" | "brick" | "sheep" | "wheat" | "stone" | "desert" | "bank";
export const RESOURCE_TYPES: ResourceType[] = ["wood", "brick", "sheep", "wheat", "stone", "desert", "bank"];

export interface Tile {
  robber: boolean;
  index: number;
  type: ResourceType;
  resource?: ResourceKey | null;
  roll?: number | null;
  corners: number[];
  pip: number;
  roads: number[];
  asset: number;
}

export interface Game {
  id: string;
  developmentCards: DevelopmentCard[];
  players: Record<string, Player>;
  sessions: Record<string, Session>;
  unselected?: any[];
  turnTimer?: any;
  debug?: boolean;
  active?: number;
  rules?: any;
  step?: number;
  placements: Placements;
  turn: Turn;
  pipOrder: number[];
  tileOrder: number[];
  resources?: number;
  pips?: any[];
  dice?: number[];
  chat?: any[];
  activities?: any[];
  playerOrder: PlayerColor[];
  state?: string;
  robber?: number;
  robberName?: string;
  turns: number;
  longestRoad?: string | false;
  longestRoadLength?: number;
  borderOrder: number[];
  largestArmy?: string | false;
  largestArmySize?: number;
  mostPorts: PlayerColor | null;
  mostPortCount: number;
  mostDeveloped?: string | false;
  private?: boolean;
  created?: number;
  lastActivity?: number;
  signature?: string;
  animationSeeds?: number[];
  startTime?: number;
  direction?: "forward" | "backward";
  winner?: string | false;
  history?: any[];
  createdAt?: string;
}

export type GameId = string;

export type IncomingMessage = { type: string | null; data: any };
