import type { Dragodinde } from './Dragodinde';
import { createDragodinde } from './Dragodinde';
import type { GaugeType } from '@domain/value-objects/GaugeType';

export const MAX_DD = 10;
export const MAX_ENCLOS = 6;
export const MAX_GAUGES = 2;

export interface GaugeRecharge {
  atSec: number;   // secondes écoulées au moment de la recharge
  level: number;   // nouveau niveau de jauge
}

export interface TimerData {
  running: boolean;
  startTime: number | null;
  pausedAt: number | null;
  pausedMs: number;
  snapGauges: Record<string, number>;
  snapStats: Record<string, Record<string, number>>;
  gaugeRecharges: Record<string, GaugeRecharge[]>;
}

export interface Enclos {
  readonly id: number;
  name: string;
  activeGauges: GaugeType[];
  gaugeLevels: Record<GaugeType, number>;
  dragodindes: Dragodinde[];
  nextDdId: number;
  timer: TimerData;
  alerted: Record<string, boolean>;
}

export function createEnclos(id: number, name?: string): Enclos {
  return {
    id,
    name: name ?? `Enclos ${id}`,
    activeGauges: [],
    gaugeLevels: { baffeur: 0, caresseur: 0, foudroyeur: 0, abreuvoir: 0, dragofesse: 0, mangeoire: 0 },
    dragodindes: [],
    nextDdId: 1,
    timer: { running: false, startTime: null, pausedAt: null, pausedMs: 0, snapGauges: {}, snapStats: {}, gaugeRecharges: {} },
    alerted: {},
  };
}

export function addDragodinde(enc: Enclos): Enclos {
  if (enc.dragodindes.length >= MAX_DD) return enc;
  const usedIds = new Set(enc.dragodindes.map(d => d.id));
  let newId = 1;
  while (usedIds.has(newId)) newId++;
  const dd = createDragodinde(newId);
  return { ...enc, dragodindes: [...enc.dragodindes, dd], nextDdId: newId + 1 };
}

export function removeDragodinde(enc: Enclos, ddId: number): Enclos {
  return { ...enc, dragodindes: enc.dragodindes.filter(d => d.id !== ddId) };
}
