import type { AppState, StateRepository } from '@domain/ports/StateRepository';
import { createAccouplement } from '@domain/entities/Accouplement';
import type { EventBus } from '@domain/events/EventBus';
import { RACE_GEN } from '@domain/value-objects/Race';

export interface RegisterAccouplementCommand {
  type: 'register-accouplement';
  parentA: string;
  parentB: string;
  baby: string;
  couples: number;
  babiesObtained: number;
}

export function createRegisterAccouplementHandler(state: AppState, repo: StateRepository, events: EventBus) {
  return (cmd: RegisterAccouplementCommand): void => {
    const gen = RACE_GEN[cmd.baby] ?? 0;
    const acc = createAccouplement(cmd.parentA, cmd.parentB, cmd.baby, gen, cmd.couples, cmd.babiesObtained);
    state.accouplements.push(acc);
    events.emit({ type: 'accouplement-registered', accouplement: acc });
    repo.save(state);
  };
}
