'use server';

import {
  getOpenResponses,
  getOtherResponses,
  getUserDoneResponses,
  getUserInProgressResponses,
} from '@kit/doctor/services/doctor-analysis.service';
import { getLogger } from '@kit/shared/logger';

import { doctorAction } from '../utils/doctor-action';

export const getUserDoneResponsesAction = doctorAction(
  async ({ page, pageSize }: { page: number; pageSize: number }) => {
    const logger = await getLogger();
    try {
      const data = await getUserDoneResponses({ page, pageSize });
      return { success: true, data };
    } catch (error) {
      logger.error(`Error fetching data for user completed jobs`, error);
      return { success: false, error: 'Failed to fetch data from the server.' };
    }
  },
);

export const getUserInProgressResponsesAction = doctorAction(
  async ({ page, pageSize }: { page: number; pageSize: number }) => {
    const logger = await getLogger();
    try {
      const data = await getUserInProgressResponses({ page, pageSize });
      return { success: true, data };
    } catch (error) {
      logger.error(`Error fetching data for user's in progress jobs`, error);
      return { success: false, error: 'Failed to fetch data from the server.' };
    }
  },
);

export const getOtherResponsesAction = doctorAction(
  async ({ page, pageSize }: { page: number; pageSize: number }) => {
    const logger = await getLogger();
    try {
      const data = await getOtherResponses({ page, pageSize });
      return { success: true, data };
    } catch (error) {
      logger.error(
        `Error fetching data for other analysis response jobs`,
        error,
      );
      return {
        success: false,
        error: 'Failed to fetch data from the server.',
      };
    }
  },
);

export const getOpenResponsesAction = doctorAction(
  async ({ page, pageSize }: { page: number; pageSize: number }) => {
    const logger = await getLogger();
    try {
      const data = await getOpenResponses({ page, pageSize });
      return { success: true, data };
    } catch (error) {
      logger.error(`Error fetching open analysis response jobs`, error);
      return { success: false, error: 'Failed to fetch data from the server.' };
    }
  },
);
