import { graphqlClient } from "@/graphql/client";
import {
  DASHBOARD_CONFIG,
  DASHBOARD_COURSE_SESSION_PROGRESS,
  DASHBOARD_COURSE_STATISTICS,
} from "@/graphql/queries";

import type {
  CourseProgressType,
  CourseStatisticsType,
  DashboardConfigType,
} from "@/gql/graphql";

export const fetchStatisticData = async (
  courseId: string
): Promise<CourseStatisticsType | null> => {
  try {
    console.log("fetching statistics for course ID: ", courseId);
    const res = await graphqlClient.query(DASHBOARD_COURSE_STATISTICS, { courseId });

    if (res.error) {
      console.error("Error fetching statistics for course ID:", courseId, res.error);
    }

    return res.data?.course_statistics || null;
  } catch (error) {
    console.error(`Error fetching statistics for course ID: ${courseId}`, error);
    return null;
  }
};

export const fetchProgressData = async (
  courseId: string
): Promise<CourseProgressType | null> => {
  try {
    const res = await graphqlClient.query(DASHBOARD_COURSE_SESSION_PROGRESS, {
      courseId,
    });

    if (res.error) {
      console.error("Error fetching progress for course ID:", courseId, res.error);
    }

    return res.data?.course_progress || null;
  } catch (error) {
    console.error(`Error fetching progress for course ID: ${courseId}`, error);
    return null;
  }
};
export const fetchDashboardConfig = async (): Promise<DashboardConfigType[] | null> => {
  try {
    const res = await graphqlClient.query(DASHBOARD_CONFIG, {});

    if (res.error) {
      console.error("Error fetching dashboard config:", res.error);
    }

    return res.data?.dashboard_config || null;
  } catch (error) {
    console.error("Error fetching dashboard config:", error);
    return null;
  }
};
