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

export interface OrganizationConfig {
  id: string;
  canStartReceivingRecord: boolean;
  canCreateBranchByItself?: boolean;
  maximumNumberOfUnits?: number;
  organizationId: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface OrganizationConfigPayload {
  canStartReceivingRecord?: boolean;
  canCreateBranchByItself?: boolean;
  maximumNumberOfUnits?: number;
  organizationId: string;
}

export interface OrganizationConfigListResponse {
  count: number;
  items: OrganizationConfig[];
}

export interface GlobalOrgConfig {
  numberOfSubOrganizations: number;
  numberOfEmployeesPerOrganization: number;
  organizationId: string;
}

export interface GlobalUnitConfig {
  numberOfSubUnits: number;
  numberOfEmployeesPerUnit: number;
  unitId: string;
}

// Create organization configuration
export const createOrganizationConfig = async (
  data: OrganizationConfigPayload,
): Promise<AxiosResponse<OrganizationConfig>> => {
  return axiosInstance.post("/organization-configurations", data, {
    headers: withHeaders(),
  });
};

// Update organization configuration
export const updateOrganizationConfig = async (
  id: string,
  data: OrganizationConfigPayload,
): Promise<AxiosResponse<OrganizationConfig>> => {
  return axiosInstance.put(`/organization-configurations/${id}`, data, {
    headers: withHeaders(),
  });
};

// Get organization configuration by ID
export const getOrganizationConfigById = async (
  id: string,
): Promise<AxiosResponse<OrganizationConfig>> => {
  return axiosInstance.get(`/organization-configurations/${id}`, {
    headers: withHeaders(),
  });
};

// Get organization configuration by organization ID
export const getOrganizationConfig = async (
  organizationId: string,
): Promise<AxiosResponse<OrganizationConfigListResponse>> => {
  return axiosInstance.get(
    `/organization-configurations/list/${organizationId}`,
    {
      headers: withHeaders(),
    },
  );
};

// Delete organization configuration
export const deleteOrganizationConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/organization-configurations/${id}`, {
    headers: withHeaders(),
  });
};

export const createGlobalOrgConfig = async (
  data: GlobalOrgConfig,
): Promise<AxiosResponse> => {
  return axiosInstance.post(`/organization-global-configurations`, data, {
    headers: withHeaders(),
  });
};

export const updateGlobalOrgConfig = async (
  id: string,
  data: GlobalOrgConfig,
): Promise<AxiosResponse> => {
  return axiosInstance.put(`/organization-global-configurations/${id}`, data, {
    headers: withHeaders(),
  });
};

export const getGlobalOrgConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organization-global-configurations/${id}`, {
    headers: withHeaders(),
  });
};

export const getListOfGlobalOrgConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organization-global-configurations/list/${id}`, {
    headers: withHeaders(),
  });
};

export const createGlobalUnitConfig = async (
  data: GlobalUnitConfig,
): Promise<AxiosResponse> => {
  return axiosInstance.post(`/global-unit-configurations`, data, {
    headers: withHeaders(),
  });
};

export const updateGlobalUnitConfig = async (
  id: string,
  data: GlobalUnitConfig,
): Promise<AxiosResponse> => {
  return axiosInstance.put(`/global-unit-configurations/${id}`, data, {
    headers: withHeaders(),
  });
};

export const getGlobalUnitConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/global-unit-configurations/${id}`, {
    headers: withHeaders(),
  });
};

export const getListOfGlobalUnitConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/global-unit-configurations/list/${id}`, {
    headers: withHeaders(),
  });
};

export const deleteGlobalUnitConfig = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/global-unit-configurations/${id}`, {
    headers: withHeaders(),
  });
};
