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

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

export interface OrganizationAdminPayload {
  organizationId: string;
  username: string;
  email: string;
  name: {
    am: string;
    en: string;
  };
  phoneNumber: string;
}

export interface UnitAdminPayload {
  unitId: string;
  username: string;
  email: string;
  name: {
    am: string;
    en: string;
  };
  phoneNumber: string;
}

export interface OrganizationAdmin {
  id: string;
  organizationId: string;
  username: string;
  email: string;
  name: {
    am: string;
    en: string;
  };
  status: string;
}

export interface OrgAdminQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
}

export const getOrganizationAdmins = async (
  params?: OrgAdminQueryParams
): Promise<AxiosResponse> => {
  return axiosInstance.get("/organizations/org-admins", {
    ...{ headers: withHeaders() },
    params,
  });
};

export const getOrganizationAdminById = async (
  id: string,
  params?: OrgAdminQueryParams
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/org-admins/${id}`, {
    ...{ headers: withHeaders() },
    params,
  });
};
export const fetchUnitAdminById = async (
  id: string,
  params?: OrgAdminQueryParams
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/units/unit-admins/${id}`, {
    ...{ headers: withHeaders() },
    params,
  });
};

export const fetchAllUnitAdminById = async (
  id: string,
  params?: OrgAdminQueryParams
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/organizations/all-admins/${id}`, {
    ...{ headers: withHeaders() },
    params,
  });
};

export const assignOrganizationAdmin = async (
  data: OrganizationAdminPayload
): Promise<AxiosResponse> => {
  return axiosInstance.post("/organizations/org-admin", data, {
    headers: withHeaders(),
  });
};

export const assignUnitAdmin = async (
  data: UnitAdminPayload
): Promise<AxiosResponse> => {
  return axiosInstance.post("/units/unit-admin", data, {
    headers: withHeaders(),
  });
};

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