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

export interface CreateEmployeePlanDto {
  planId: string;
  employeePositionId: string;
  serviceId?: string;
  parentEmployeePlanId?: string;
  expectedQuantity: number;
  planType: "number" | "percent" | "boolean";
  timeframe: string;
  week?: number;
  month?: number;
  quarter?: number;
}
export interface EmployeeDTO {
  createdAt: string;
  updatedAt: string;
  id: string;
  isCurrent: boolean;
  status: string;
  name: LocalizedName;
  organizationId: string;
  unitId: string;
  userId: string;
  employeePositions: EmployeePositionDTO[];
}

export interface LocalizedName {
  am: string;
  en: string;
}

export interface planParam {
  timeframe: string;
  skip?: number;
  take?: number;
}

export interface EmployeePositionDTO {
  createdAt: string;
  updatedAt: string;
  id: string;
  isDelegate: boolean;
  isCurrent: boolean;
  startDate: string;
  amharicStartDate: string;
  endDate: string | null;
  amharicEndDate: string | null;
  unitId: string;
  delegatorId: string | null;
  employeeId: string;
  positionId: string;
  delegationLetterId: string | null;
  position: PositionDTO;
}

export interface PositionDTO {
  createdAt: string;
  updatedAt: string;
  id: string;
  name: LocalizedName;
  key: string;
  rank: number;
  parentPositionId: string | null;
  unitId: string;
  organizationId: string;
  projectId: string | null;
  positionTypeId: string;
}

export interface ImmediateChildResponse {
  count: number;
  items: EmployeeDTO[];
}

export const createEmployeePlan = async (
  data: CreateEmployeePlanDto
): Promise<AxiosResponse> => {
  return performanceInstance.post("/employee-plans", data, {
    headers: withHeaders(),
  });
};

export const getEmployeePlans = async (
  planId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/employee-plans/list/${planId}`, {
    headers: withHeaders(),
  });
};

export const getEmployeePlan = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.get(`/employee-plans/${id}`, {
    headers: withHeaders(),
  });
};

export const updateEmployeePlan = async (
  id: string,
  data: CreateEmployeePlanDto
): Promise<AxiosResponse> => {
  return performanceInstance.put(`/employee-plans/${id}`, data, {
    headers: withHeaders(),
  });
};

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

export const getPlansByUnit = async (
  unitId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plans/mine`, {
    headers: withHeaders(),
    params: { unitId },
  });
};

export const getPositionsByUnit = async (
  unitId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/positions`, {
    headers: withHeaders(),
    params: { unitId },
  });
};

export const getEmployeePositions = async (
  positionId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/employee-positions`, {
    headers: withHeaders(),
    params: { positionId },
  });
};

export const getServices = async (): Promise<AxiosResponse> => {
  return performanceInstance.get(`/services`, {
    headers: withHeaders(),
  });
};

export const getParentEmployeePlanDetail = async (
  id: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/employee-plans/${id}`, {
    headers: withHeaders(),
  });
};

export const fetchImmediateChild =
  async (): Promise<ImmediateChildResponse> => {
    try {
      const { data } = await axiosInstance.get(`/employees/immediate-child`, {
        headers: withHeaders(),
      });
      return data;
    } catch (error: any) {
      throw new Error(
        error?.response?.data?.message || "Failed to fetch immediate child"
      );
    }
  };
export const getEmployeePlanByPlanId = async (
  planId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(
    `/employee-plans/plan/${planId}/employee-plans`,
    {
      headers: withHeaders(),
    }
  );
};

export const getEmployeeSubPlanByPlanId = async (
  planId: string
): Promise<AxiosResponse> => {
  return performanceInstance.get(
    `/employee-plans/${planId}/sub-employee-plans`,
    {
      headers: withHeaders(),
    }
  );
};

export const getMyEmployeePlan = async (
  params?: planParam
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/employee-plans/mine`, {
    headers: withHeaders(),
    params,
  });
};
