import { apiClient } from './client';
import type { Simulation, SimulationCreateInput, SimulationPatchInput, SimulationStatus } from './types';

export async function listSimulations(engagementId: number): Promise<Simulation[]> {
  const { data } = await apiClient.get<Simulation[]>(`/engagements/${engagementId}/simulations`);
  return data;
}

export async function createSimulation(
  engagementId: number,
  input: SimulationCreateInput,
): Promise<Simulation> {
  const { data } = await apiClient.post<Simulation>(`/engagements/${engagementId}/simulations`, input);
  return data;
}

export async function getSimulation(id: number): Promise<Simulation> {
  const { data } = await apiClient.get<Simulation>(`/simulations/${id}`);
  return data;
}

export async function updateSimulation(id: number, patch: SimulationPatchInput): Promise<Simulation> {
  const { data } = await apiClient.patch<Simulation>(`/simulations/${id}`, patch);
  return data;
}

export async function deleteSimulation(id: number): Promise<void> {
  await apiClient.delete(`/simulations/${id}`);
}

export async function transitionSimulation(
  id: number,
  to: Extract<SimulationStatus, 'review_required' | 'done'>,
): Promise<Simulation> {
  const { data } = await apiClient.post<Simulation>(`/simulations/${id}/transition`, { to });
  return data;
}
