import { api } from "./api";

export type AiInteraction = {
  id: number;
  module: string;
  mode: string | null;
  title: string;
  provider: string;
  result: { text?: string };
  createdAtUtc: string;
};

export type AiUsage = {
  currentMonth: { calls: number; inputCharacters: number; outputCharacters: number; estimatedTokens: number };
  allTime: { calls: number; inputCharacters: number; outputCharacters: number; estimatedTokens: number };
};

export const AI_MODULES: { key: string; label: string; blurb: string }[] = [
  { key: "job-analysis", label: "Job Analysis", blurb: "Break down the advert: skills, requirements, salary, work model, interview topics." },
  { key: "career-match", label: "Career Match", blurb: "Your profile vs the advert: strengths, gaps, match %, suggested improvements." },
  { key: "cover-letter", label: "Cover Letter", blurb: "A tailored cover letter in the tone you choose." },
  { key: "interview", label: "Interview Prep", blurb: "Likely questions, STAR answers, company research, a checklist." },
  { key: "application-review", label: "Application Review", blurb: "Grammar, gaps, weak areas, ATS issues, overall strength." },
];

export const COVER_LETTER_MODES = ["professional", "friendly", "short", "detailed", "modern", "traditional"];

export const aiWorkspaceApi = {
  usage: () => api.get<AiUsage>("/ai/usage").then((r) => r.data),
  modules: (jobId: number) => api.get<{ modules: string[]; provider: string }>(`/jobapplications/${jobId}/ai/modules`).then((r) => r.data),
  generate: (jobId: number, body: { module: string; mode?: string; extraContext?: string; targetLanguage?: "en" | "nb-NO"; currentText?: string; action?: string }) =>
    api.post<AiInteraction>(`/jobapplications/${jobId}/ai/generate`, body).then((r) => r.data),
  history: (jobId: number, module?: string) =>
    api.get<AiInteraction[]>(`/jobapplications/${jobId}/ai/history`, { params: { module } }).then((r) => r.data),
  remove: (jobId: number, id: number) => api.delete(`/jobapplications/${jobId}/ai/history/${id}`),
};
