import performanceInstance from "@/shared/services/performanceInstance";
import { withHeaders } from "@/record-management/services/api/withHeaders";
import { AxiosResponse } from "axios";

export interface ServiceAssignment {
  id: string;
  positionId: string;
  serviceId: string;
  assignedAt: string;
}

export interface AssignServicesPayload {
  positionId: string;
  serviceIds: string[];
}

/**
 * Fetch services assigned to a specific position
 * Returns the service (second) assigned to a position (first)
 */
export const getAssignedServices = async (
  positionId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(
    `/position-services/seconds-for-first/${positionId}`,
    {
      headers: withHeaders(),
    }
  );
};

/**
 * Assign service(s) to a position
 * API expects: firstIds (positions array) and secondId (service)
 */
export const assignServicesToPosition = async (
  positionId: string,
  serviceIds: string[]
): Promise<AxiosResponse> => {
  return performanceInstance.post(
    `/position-services/assign-firsts-for-second`,
    {
      firstIds: [positionId],
      secondId: serviceIds[0], // Single service ID
    },
    {
      headers: withHeaders(),
    }
  );
};

/**
 * Remove a service assignment from a position
 */
export const removeServiceFromPosition = async (
  positionId: string,
  serviceId: string
): Promise<AxiosResponse> => {
  return performanceInstance.delete(
    `/positions/${positionId}/services/${serviceId}`,
    {
      headers: withHeaders(),
    }
  );
};

/**
 * Get all services for a specific organization
 */
export const getServicesByOrganization = async (
  organizationId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/services/list/${organizationId}`, {
    headers: withHeaders(),
  });
};

// Keep backward compatibility
export const getServicesByUnit = getServicesByOrganization;

/**
 * Get all services (active and published) - deprecated, use getServicesByUnit
 */
export const getAvailableServices = async (): Promise<AxiosResponse> => {
  return performanceInstance.get("/services", {
    headers: withHeaders(),
    params: {
      isActive: true,
      status: "PUBLISHED",
    },
  });
};

/**
 * Get services associated with a position (using position endpoint)
 */
export const getPositionServices = async (
  positionId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/position-services/position/${positionId}`, {
    headers: withHeaders(),
  });
};

/**
 * Get position services by position ID (for working days configuration)
 */
export const getPositionServicesByPositionId = async (
  positionId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/position-services/position/${positionId}`, {
    headers: withHeaders(),
  });
};

/**
 * Delete a position service by ID
 */
export const deletePositionService = async (
  positionServiceId: string
): Promise<AxiosResponse> => {
  return performanceInstance.delete(`/position-services/${positionServiceId}`, {
    headers: withHeaders(),
  });
};
