/*
 * work 화면 전용 API — G-06 회의 녹음 STT·AI 회의록, G-07 자연어 통합검색.
 * 공용 api/endpoints.ts 를 건드리지 않도록 work 화면 전용으로 분리한다(계약은 백엔드 work 라우터와 정합).
 */
import { api } from '../../api/client';
import type { SearchResultItem } from '../../api/types';

// ── G-06 회의 녹음 STT · AI 회의록 ──
export interface AudioUploadResponse {
  audioId: string;
  filename: string;
  sizeBytes: number;
}
export interface SttResponse {
  transcript: string;
  degraded: boolean;
}
export interface MinutesActionDraft {
  content: string;
  assignee?: string | null;
  dueDate?: string | null;
}
export interface GeneratedMinutes {
  summary: string;
  decisions: string[];
  actionItems: MinutesActionDraft[];
  minutesText: string;
  degraded: boolean;
  provider: string;
}

const MEET = '/api/work/meetings';

export const meetingAiApi = {
  uploadAudio: (file: File) => {
    const form = new FormData();
    form.append('file', file);
    return api.postForm<AudioUploadResponse>(`${MEET}/audio`, form);
  },
  stt: (audioId: string) => api.post<SttResponse>(`${MEET}/stt`, { audioId }),
  generateMinutes: (body: { title: string; transcript?: string; attendees?: string[] }) =>
    api.post<GeneratedMinutes>(`${MEET}/minutes/generate`, body),
  addActionsBulk: (id: string, items: MinutesActionDraft[]) =>
    api.post<unknown>(`${MEET}/${encodeURIComponent(id)}/actions/bulk`, items),
  /** 회의록 + 전사 함께 저장(transcript 선택). */
  saveMinutes: (id: string, minutes: string, transcript?: string) =>
    api.put<void>(`${MEET}/${encodeURIComponent(id)}/minutes`, { minutes, transcript }),
};

// ── G-07 자연어 통합검색 ──
export interface AiSearchResult {
  question: string;
  keywords: string[];
  types: string[];
  summary: string | null;
  results: SearchResultItem[];
  total: number;
  degraded: boolean;
  provider: string;
}

export const searchAiApi = {
  ask: (q: string, limit = 50) => api.post<AiSearchResult>('/api/work/search/ai', { q, limit }),
};
