import axios from "axios";
import { withHeaders } from "./withHeaders";
import chronicleInstance from "@/shared/services/chronicleInstance";

export enum DashaboardParam {
  POSITION = "position",
  EMPLOYEE = "employee",
}

interface dashboardParam {
  params?: Record<string, any>;
}
interface PermittedDashbaordName {
  am: string;
  en: string;
}
interface Description {
  am: string;
  en: string;
}
export interface PermittedDashboardItem {
  createdAt: string;
  updatedAt: string;
  tenantId: string;
  id: string;
  name: PermittedDashbaordName;
  metabaseDashboardNumber: number;
  description: Description;
  permissionId: string;
  params?: DashaboardParam;
}

export interface ParameterItem {
  createdAt: string;
  updatedAt: string;
  tenantId: string;
  id: string;
  key: string;
  parameterType: string;
  dateFilterOperator: string;
  stringFilterOperator: string;
  numberFilterOperator: string;
  timeGroupingOption: string;
  isArray: boolean;
  optionsApi: string;
  optionsSelectedField: string;
  optionsDisplayColumns: string[];
  valueOptions: ValueOption[];
  metabaseDashboardId: string;
}

export interface ValueOption {
  am: string;
  en: string;
  value: string;
}

export interface ParameterResponse {
  count: number;
  items: ParameterItem[];
}

interface PermittedDashboardResponse {
  count: number;
  items: PermittedDashboardItem[];
}
export const fetchListOfPermittedDashboards =
  async (): Promise<PermittedDashboardResponse> => {
    try {
      const response = await chronicleInstance.get(
        `/metabase-dashboards/permitted`,
        {
          headers: withHeaders(),
        }
      );
      return response.data;
    } catch (error) {
      console.error("Error fetching List of Permitted Dashboards.", error);
      throw error;
    }
  };

export const fetchDashboardUrl = async (
  body: dashboardParam,
  id: string
): Promise<any> => {
  try {
    const response = await chronicleInstance.post(
      `/metabase-dashboards/${id}/iframe-url`,
      body,
      {
        headers: withHeaders(),
      }
    );
    return response.data;
  } catch (error) {
    console.error("Error fetching Dashboard.", error);
    throw error;
  }
};

export const fetchListOfParameters = async (
  dashboardId: string
): Promise<ParameterResponse> => {
  try {
    const response = await chronicleInstance.get(
      `/metabase-dashboard_parameters/list/${dashboardId}`,
      {
        headers: withHeaders(),
      }
    );
    return response.data;
  } catch (error) {
    console.error("Error fetching List of Permitted Dashboards.", error);
    throw error;
  }
};
