import axios from "@/shared/services/axiosInstance";
import { DashboardStatsDto } from "@/super-admin/components/dashboard/types/dashboardStatsDto";
import axiosInstance from "../../../shared/services/axiosInstance";
import { AxiosResponse, AxiosError } from "axios";

// This is your DTO structure
export const getDashboardStats = async (): Promise<DashboardStatsDto> => {
  const { data } = await axios.get("/dashboard/stats");
  return data;
};

// Types for organization report data
export interface OrganizationReport {
  unitsCount: number;
  employeesCount: number;
  positionsCount: number;
  totalDocuments?: number;
  recentActivities?: {
    id: string;
    type: string;
    description: string;
    timestamp: string;
  }[];
}

export type UnitReport = Omit<OrganizationReport, "unitsCount"> & {
  unitsCount?: number;
};

/**
 * Fetches organization report data for the dashboard
 * @param organizationId - The ID of the organization
 * @returns Promise with the organization report data
 */
export const getOrganizationReport = async (
  organizationId: string
): Promise<AxiosResponse<OrganizationReport>> => {
  try {
    return await axiosInstance.get(`/organizations/${organizationId}/report`);
  } catch (error: unknown) {
    if (error instanceof AxiosError && error.response) {
      // Handle specific error cases if needed
    }
    throw error;
  }
};

export const getUnitReport = async (
  organizationId: string,
  unitId: string,
): Promise<AxiosResponse<UnitReport>> => {
  return axiosInstance.get(
    `/organizations/${organizationId}/report/${unitId}/unit`,
  );
};

/**
 * Fetches total employees count for an organization
 * @param organizationId - The ID of the organization
 * @returns Promise with the total employees count
 */
export const getOrganizationEmployees = async (
  organizationId: string
): Promise<AxiosResponse<{ total: number }>> => {
  return axiosInstance.get(`/organizations/${organizationId}/employees/count`);
};

/**
 * Fetches total departments count for an organization
 * @param organizationId - The ID of the organization
 * @returns Promise with the total departments count
 */
export const getOrganizationDepartments = async (
  organizationId: string
): Promise<AxiosResponse<{ total: number }>> => {
  return axiosInstance.get(
    `/organizations/${organizationId}/departments/count`
  );
};

/**
 * Fetches total units count for an organization
 * @param organizationId - The ID of the organization
 * @returns Promise with the total units count
 */
export const getOrganizationUnits = async (
  organizationId: string
): Promise<AxiosResponse<{ total: number }>> => {
  return axiosInstance.get(`/organizations/${organizationId}/units/count`);
};
