import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "./axiosInstance";
import { AxiosResponse } from "axios";
import {
  DocumentRequirementDto,
  ResponseActionDto,
} from "../dto/External-Portal/External-PortalDto";

export interface OrgHeaders {
  tenantKey: string;
  unitId?: string;
}

export interface OrganizationPayload {
  name: {
    am: string;
    en: string;
  };
  key: string;
  organizationTypeId: string;
  parentId?: string;
  isGovernmentOrganization: boolean;
}

export interface OrgQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
  organizationTypeKey?: string;
  name?: string;
}

export enum FilterEnum {
  EXTERNAL = "organization",
  INDIVIDUAL = "individual",
}

export const getOrganizations = async (
  params?: OrgQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get("/organizations/filter", {
    ...{ headers: withHeaders() },
    params,
  });
};

export const getOrganizationsWithAdminFlag = async (
  params?: OrgQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get("/organizations/with-admin-flag", {
    ...{ headers: withHeaders() },
    params: {
      take: 100,
      ...params,
    },
  });
};
export const getMyAdminOrganizations = getOrganizationsWithAdminFlag;

export const getOrganizationById = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/${id}`, { headers: withHeaders() });
};

export const getChildren = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/${id}/children`, {
    headers: withHeaders(),
  });
};

export const getEmployeesUnderOrg = async (
  id: string | number,
  params?: OrgQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/current/${id}/employees`, {
    headers: withHeaders(),
    params: {
      take: 1000, // Get a large number of employees by default
      skip: 0,
      ...params,
    },
  });
};

export const getEmployeeCountByOrgId = async (
  id: string | number,
): Promise<AxiosResponse> => {
  // Try the endpoint from your curl example
  return axiosInstance.get(`/organizations/${id}/employees/count`, {
    headers: withHeaders(),
  });
};

export const createOrganization = async (
  data: OrganizationPayload,
): Promise<AxiosResponse> => {
  return axiosInstance.post("/organizations", data, { headers: withHeaders() });
};

export const activateOrganization = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/organizations/${id}/activate`, null, {
    headers: withHeaders(),
  });
};

export const deActivateOrganization = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/organizations/${id}/debar`, null, {
    headers: withHeaders(),
  });
};

export const updateOrganization = async (
  id: string | number,
  data: OrganizationPayload,
): Promise<AxiosResponse> => {
  return axiosInstance.put(`/organizations/${id}`, data, {
    headers: withHeaders(),
  });
};

export const deleteOrganization = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/organizations/${id}`, {
    headers: withHeaders(),
  });
};

export const softDeleteOrganization = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/organizations/${id}/soft`, {
    headers: withHeaders(),
  });
};

// NOTE: Speculative endpoints — backend not yet implemented. Will 404 until
// the API team adds:
//   GET   /organizations/archived
//   PATCH /organizations/{id}/restore
export const getArchivedOrganizations = async (
  params?: OrgQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/archived`, {
    headers: withHeaders(),
    params,
  });
};

export const restoreOrganization = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/organizations/${id}/restore`, null, {
    headers: withHeaders(),
  });
};

export const getDocumentRequirements = async (): Promise<AxiosResponse> => {
  return axiosInstance.get(`/documentary-requirements`, {
    headers: withHeaders(),
  });
};
export const getDocumentRequirementsById = async (
  id: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/documentary-requirements/${id}`, {
    headers: withHeaders(),
  });
};
export const getDocumentRequirementsByFilter = async (
  filter: FilterEnum,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/documentary-requirements/${filter}/type`);
};

export const postDocumentRequirements = async (
  data: DocumentRequirementDto,
): Promise<AxiosResponse> => {
  return axiosInstance.post(`/documentary-requirements`, data, {
    headers: withHeaders(),
  });
};

export const giveResponse = async (
  id: string,
  data: ResponseActionDto,
): Promise<AxiosResponse> => {
  return axiosInstance.post(`user-documents/${id}/response`, data, {
    headers: withHeaders(),
  });
};

export const getArchivedUserId = async (
  unitId: string,
  params?: OrgQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/employees/archived/${unitId}/with-unit`, {
    headers: withHeaders(),
    params,
  });
};
