import type { Draft } from "./model/editorTypes";

export type SaveEditedPlanRequest = {
  planId: string;
  draft: Draft;
};

export type SaveEditedPlanResult = {
  saved: false;
  message: string;
};

export async function saveEditedPlan(_request: SaveEditedPlanRequest): Promise<SaveEditedPlanResult> {
  // TODO(backend): эндпоинт сохранения правок плана отсутствует в pcp-tgu-service.
  return {
    saved: false,
    message: "Сохранение правок плана во внешний сервис ещё не реализовано в backend."
  };
}

export type PlanSurveyMode = {
  id: number;
  planId: number;
  timeStart: string | null;
  revolution: number;
  type: "SURVEY";
  status: string;
  lat: number;
  longitude: number;
  duration: number;
  contourWkt: string | null;
  roll: number;
};

export type PlanDropMode = {
  id: number;
  planId: number;
  timeStart: string | null;
  revolution: number;
  type: "DROP";
  station: string | null;
  duration: number;
  surveys: number[];
};

export type PlanMode = PlanSurveyMode | PlanDropMode;

export async function fetchPlanModes(planId: string): Promise<PlanMode[]> {
  const response = await fetch(`/api/current-plans/missions/${encodeURIComponent(planId)}/modes`, {
    headers: { Accept: "application/json" }
  });
  if (!response.ok) {
    throw new Error(`Ошибка загрузки включений плана: ${response.status}`);
  }
  return response.json() as Promise<PlanMode[]>;
}
