/*
 * M6 서류·마일스톤(SCR-22/23) 실배선 API 계약.
 * 공용 클라이언트(../../api/client)만 사용 — client.ts·endpoints.ts·types.ts 무수정.
 * 정본: 백엔드 DocumentController 시그니처
 *   - GET  /api/events/{eventId}/milestones          → ApiResponse<MilestoneRow[]>
 *   - GET  /api/events/{eventId}/documents            → ApiResponse<RequiredDocRow[]>
 *   - GET  /api/events/{eventId}/documents/review     → ApiResponse<DocReview>
 *   - POST /api/events/{eventId}/documents/{docType}  → ApiResponse<RequiredDocRow>  (action: save|submit)
 * ★ HWP/PDF 렌더는 파일 큐 후속 — 엔드포인트 없음(프론트 버튼 disabled 유지).
 */
import { api } from '../../api/client';

export type MilestoneState = 'done' | 'active' | 'todo';
export interface MilestoneRow {
  label: string;
  sub: string;
  state: MilestoneState;
  dueDate: string | null;
}

export type DocStatus = 'pending' | 'draft' | 'submitted' | 'approved' | 'rejected';
export interface RequiredDocRow {
  docType: string;
  name: string;
  status: DocStatus;
  dueDate: string | null;
  dday: number | null;
  sortOrder: number;
}

export interface ReviewIssueRow {
  tone: 'warn' | 'info';
  title: string;
  description: string;
}
export interface DocReview {
  issues: ReviewIssueRow[];
  progressPct: number;
}

/** save→임시저장(draft), submit→제출(submitted). */
export type DocAction = 'save' | 'submit';

export const docsApi = {
  milestones: (eventId: string) =>
    api.get<MilestoneRow[]>(`/api/events/${encodeURIComponent(eventId)}/milestones`),
  documents: (eventId: string) =>
    api.get<RequiredDocRow[]>(`/api/events/${encodeURIComponent(eventId)}/documents`),
  review: (eventId: string) =>
    api.get<DocReview>(`/api/events/${encodeURIComponent(eventId)}/documents/review`),
  transition: (eventId: string, docType: string, action: DocAction) =>
    api.post<RequiredDocRow>(
      `/api/events/${encodeURIComponent(eventId)}/documents/${encodeURIComponent(docType)}`,
      { action },
    ),
};
