export interface HostConfig {
  providerId: string;
  baseUrl: string;
  enabled: boolean;
}

export interface FleetState {
  hosts: Map<string, HostState>;
}

export interface HostState {
  providerId: string;
  liveness: 'connected' | 'down';
  lastSeenAt: Date | null;
  seq: number;
  /** Host-level inflight total (the fork's SSE publishes only a total, not per-model). */
  inflightTotal: number;
  models: Map<string, ModelState>;
}

export interface ModelState {
  model: string;
  state: string;
  ts: Date;
  ttlDeadline: Date | null;
  inflight: number;
}

export interface SnapshotData {
  hosts: Array<{
    providerId: string;
    liveness: 'connected' | 'down';
    lastSeenAt: string | null;
    seq: number;
    models: Array<{
      model: string;
      state: string;
      ts: string;
      ttlDeadline: string | null;
      inflight: number;
    }>;
  }>;
  requests?: Array<{
    id: number;
    providerId: string;
    ts: string;
    model: string | null;
    reqPath: string | null;
    statusCode: number | null;
    durationMs: number | null;
  }>;
  perfSamples?: Array<{
    providerId: string;
    ts: string;
    gpu: unknown;
    sys: unknown;
  }>;
}

export function createFleetState(): FleetState {
  return { hosts: new Map() };
}

export function ensureHostState(fleet: FleetState, providerId: string): HostState {
  let state = fleet.hosts.get(providerId);
  if (!state) {
    state = {
      providerId,
      liveness: 'down',
      lastSeenAt: null,
      seq: 0,
      inflightTotal: 0,
      models: new Map(),
    };
    fleet.hosts.set(providerId, state);
  }
  return state;
}

export function stampLastSeen(state: HostState): void {
  state.lastSeenAt = new Date();
}

export function incrementSeq(state: HostState): number {
  state.seq += 1;
  return state.seq;
}
