import { SectorListParam } from "@/performance-management/types/planTypes";
import PlanType from "@/performance-management/utils/plan-unit";
import { withHeaders } from "@/record-management/services/api/withHeaders";
import performanceInstance from "@/shared/services/performanceInstance";
import { AxiosResponse } from "axios";

export interface PlanParams {
  serviceId?: string;
  positionId?: string;
  parentPlanId?: string;
  skip?: number;
  take?: number;
  orderBy?: string;
  planFor?: "team" | "self" | "position";
}

export interface CreatePlanDto {
  id?: string;
  name: {
    am: string;
    en: string;
  };

  description: {
    am: string;
    en: string;
  };

  positionId?: string;
  parentPlanId?: string; //
  planType: PlanType; //
  planYearId: string; //
  // initialGoal: number;
  // expectedGoal: number;
  weight: number;
  month?: number;
  week?: number;
  day?: number;
  quarter?: number;
  timeframe: string;
}

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

export const updatePlan = async (
  data: CreatePlanDto,
): Promise<AxiosResponse> => {
  return performanceInstance.put(`/plans/${data?.id}`, data, {
    headers: withHeaders(),
  });
};

export const requestApproval = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.post(
    `/plans/${id}/sent`,
    {},
    {
      headers: withHeaders(),
    },
  );
};
export const approvePlan = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.post(
    `/plans/${id}/approve`,
    {},
    {
      headers: withHeaders(),
    },
  );
};
export const rejectPlan = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.post(
    `/plans/${id}/reject`,
    {},
    {
      headers: withHeaders(),
    },
  );
};
export const deletePlan = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.delete(`/plans/${id}`, {
    headers: withHeaders(),
  });
};

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

export const getPlanList = async (
  id: string,
  params?: PlanParams,
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plans/list/${id}`, {
    headers: withHeaders(),
    params,
  });
};

export const getSectorList = async (
  id?: string,
  params?: SectorListParam,
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plans/${id}/sector`, {
    headers: withHeaders(),
    params,
  });
};

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

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

export const getMySubPlans = async (
  id: string,
  params?: SectorListParam,
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plans/${id}/mine-sub-plans`, {
    headers: withHeaders(),
    params,
  });
};

export const getTeamSubPlans = async (
  id: string,
  params?: SectorListParam,
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plans/${id}/team-sub-plans`, {
    headers: withHeaders(),
    params,
  });
};
