import { axiosInstance } from '../../axiosApi';

export type PromptRating = 'up' | 'down';

export type PromptFeedbackPayload = {
  rating: PromptRating;
  reason?: string | null;
  comment?: string | null;
};

export type PromptFeedbackResponse = PromptFeedbackPayload & {
  id?: number;
  prompt_id?: number;
};

/**
 * Upsert thumbs rating for an assistant prompt (chat_backend#67).
 */
export async function upsertPromptFeedback(
  promptId: number,
  payload: PromptFeedbackPayload,
): Promise<PromptFeedbackResponse> {
  const { data } = await axiosInstance.post<PromptFeedbackResponse>('prompt_feedback', {
    prompt_id: promptId,
    rating: payload.rating,
    reason: payload.reason ?? undefined,
    comment: payload.comment ?? undefined,
  });
  return data;
}

/**
 * Clear a vote for an assistant prompt.
 */
export async function clearPromptFeedback(promptId: number): Promise<void> {
  await axiosInstance.delete('prompt_feedback', {
    params: { prompt_id: promptId },
  });
}
