import { api } from "@/lib/api";
import { config } from "@/config/config";
import type {
  AdjustmentStatus,
  SensorPlacementScheme,
} from "./types";

export interface OptimizeSchemeInput {
  scheme_name: string;
  sensor_type: "pressure";
  method: "sensitivity" | "kmeans";
  sensor_count: number;
  min_diameter: number;
}

export const optimizeSensorPlacement = async (
  input: OptimizeSchemeInput,
): Promise<SensorPlacementScheme> => {
  const response = await api.post<SensorPlacementScheme>(
    `${config.BACKEND_URL}/api/v1/sensor-placement-optimization-runs`,
    input,
  );
  return response.data;
};

export const getSensorPlacementScheme = async (
  schemeId: number,
): Promise<SensorPlacementScheme> => {
  const response = await api.get<SensorPlacementScheme>(
    `${config.BACKEND_URL}/api/v1/sensor-placement-schemes/${schemeId}`,
  );
  return response.data;
};

export const overwriteSensorPlacementScheme = async (
  schemeId: number,
  expectedSensorLocation: string[],
  sensorLocation: string[],
): Promise<SensorPlacementScheme> => {
  const response = await api.put<SensorPlacementScheme>(
    `${config.BACKEND_URL}/api/v1/sensor-placement-schemes/${schemeId}`,
    {
      expected_sensor_location: expectedSensorLocation,
      sensor_location: sensorLocation,
    },
  );
  return response.data;
};

export const exportSensorPlacementExcel = async (
  schemeId: number,
  sensorLocation: string[],
  adjustmentStatus: Record<string, AdjustmentStatus>,
) => {
  const response = await api.post<Blob>(
    `${config.BACKEND_URL}/api/v1/sensor-placement-schemes/${schemeId}/exports/excel`,
    {
      sensor_location: sensorLocation,
      adjustment_status: adjustmentStatus,
    },
    {
      responseType: "blob",
    },
  );
  return response.data;
};
