import { Axios, AxiosResponse } from "axios";
import performanceInstance from "@/shared/services/performanceInstance";
import { withHeaders } from "@/record-management/services/api/withHeaders";
import DateMeasurementUnit from "@/performance-management/utils/data-measurement-unit";

export interface CreateServiceDto {
  id?: string; // Optional for create, required for update
  slug: string;
  prefix: string;
  name: {
    am: string;
    en: string;
  };
  description: {
    am: string;
    en: string;
  };
  organizationId: string;
  serviceCategoryId?: string;
  parentServiceId?: string;
  estimatedTime: number;
  estimatedTimeUnit: DateMeasurementUnit;
}

export const createService = async (
  data: CreateServiceDto
): Promise<AxiosResponse> => {
  return performanceInstance.post("/services", data, {
    headers: withHeaders(),
  });
};

export const getService = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.get(`/services/${id}`, {
    headers: withHeaders(),
  });
};
export const getServiceList = async (
  organizationId: string,
  serviceCategoryId?: string,
  parentServiceId?: string
): Promise<AxiosResponse> => {
  const params: any = {};
  if (serviceCategoryId) params.serviceCategoryId = serviceCategoryId;
  if (parentServiceId) params.parentServiceId = parentServiceId;

  return performanceInstance.get(`/services/list/${organizationId}`, {
    headers: withHeaders(),
    params,
  });
};

export const updateService = async (
  data: CreateServiceDto
): Promise<AxiosResponse> => {
  return performanceInstance.put(`/services/${data.id}`, data, {
    headers: withHeaders(),
  });
};

export const deleteService = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.delete(`/services/${id}`, {
    headers: withHeaders(),
  });
};
