import { invoke } from "@tauri-apps/api/core";

export type FeedbackErrorCode =
  | "invalid"
  | "rate_limit"
  | "server_error"
  | "network_error";

export interface FeedbackContext {
  page?: string;
  locale?: string;
  theme?: string;
  viewport?: string;
  userAgent?: string;
  timestamp?: string;
}

export interface FeedbackSuccess {
  id: string;
  created_at: string;
}

export interface SendFeedbackInput {
  content: string;
  userId?: string | null;
  context?: FeedbackContext;
}

export async function sendFeedback(
  input: SendFeedbackInput,
): Promise<FeedbackSuccess> {
  return invoke<FeedbackSuccess>("send_feedback", {
    content: input.content,
    userId: input.userId ?? null,
    context: input.context ?? null,
  });
}

export async function getFeedbackUserAgent(): Promise<string> {
  return invoke<string>("get_feedback_user_agent");
}

export function isFeedbackErrorCode(value: unknown): value is FeedbackErrorCode {
  return (
    value === "invalid" ||
    value === "rate_limit" ||
    value === "server_error" ||
    value === "network_error"
  );
}
