import apiEndpoints from '../apiEndpoints';
import {
  QuestionData,
  QuestionResponse,
} from '../../utils/interface/questionInterface';

export const postQuestion = async (
  questionData: QuestionData
): Promise<QuestionResponse> => {
  const response = await fetch(
    `http://localhost:3000${apiEndpoints.question}`,
    {
      method: 'POST',
      body: JSON.stringify(questionData),
      headers: {
        'Content-Type': 'application/json',
      },
    }
  );

  const data: QuestionResponse = await response.json();

  if (!response.ok) {
    throw new Error(data.message || 'Something went wrong!');
  }

  return data;
};
