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

export interface PlanYearParams {
  skip?: number;
  take?: number;
  orderBy?: string;
  isActive?: boolean;
}

// Get all plan years
export const getPlanYears = async (
  organizationId: string,
  params?: PlanYearParams
): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plan-years/list/${organizationId}`, {
    headers: withHeaders(),
    params,
  });
};

// Get single plan year by ID
export const getPlanYear = async (id: string): Promise<AxiosResponse> => {
  return performanceInstance.get(`/plan-years/${id}`, {
    headers: withHeaders(),
  });
};

// Create new plan year
export const createPlanYear = async (
  data: CreatePlanYearDto
): Promise<AxiosResponse> => {
  // return new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve({
  //       data: { ...data, id: `py-${data.year}` },
  //       status: 201,
  //       statusText: "Created",
  //       headers: {},
  //       config: {} as any,
  //     });
  //   }, 500);
  // });

  return performanceInstance.post("/plan-years", data, {
    headers: withHeaders(),
  });
};

// Update existing plan year
export const updatePlanYear = async (
  data: CreatePlanYearDto
): Promise<AxiosResponse> => {
  // return new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve({
  //       data: data,
  //       status: 200,
  //       statusText: "OK",
  //       headers: {},
  //       config: {} as any,
  //     });
  //   }, 500);
  // });

  return performanceInstance.put(`/plan-years/${data.id}`, data, {
    headers: withHeaders(),
  });
};

// Delete plan year
export const deletePlanYear = async (id: string): Promise<AxiosResponse> => {
  // return new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve({
  //       data: { success: true },
  //       status: 200,
  //       statusText: "OK",
  //       headers: {},
  //       config: {} as any,
  //     });
  //   }, 500);
  // });

  return performanceInstance.delete(`/plan-years/${id}`, {
    headers: withHeaders(),
  });
};
