import apiClient from './authApi';

export interface ConclusionRemark {
  conclusionId: string;
  requestId: string;
  aiGeneratedRemark: string | null;
  aiModelUsed: string | null;
  aiConfidenceScore: number | null;
  finalRemark: string | null;
  editedBy: string | null;
  isEdited: boolean;
  editCount: number;
  approvalSummary: any;
  documentSummary: any;
  keyDiscussionPoints: string[];
  generatedAt: string | null;
  finalizedAt: string | null;
  createdAt: string;
  updatedAt: string;
}

/**
 * Generate AI-powered conclusion remark
 */
export async function generateConclusion(requestId: string): Promise<{
  conclusionId: string;
  aiGeneratedRemark: string;
  keyDiscussionPoints: string[];
  confidence: number;
  generatedAt: string;
}> {
  const response = await apiClient.post(`/conclusions/${requestId}/generate`);
  return response.data.data;
}

/**
 * Update conclusion remark (edit by initiator)
 */
export async function updateConclusion(requestId: string, finalRemark: string): Promise<ConclusionRemark> {
  const response = await apiClient.put(`/conclusions/${requestId}`, { finalRemark });
  return response.data.data;
}

/**
 * Finalize conclusion and close request
 */
export async function finalizeConclusion(requestId: string, finalRemark: string): Promise<{
  conclusionId: string;
  requestNumber: string;
  status: string;
  finalRemark: string;
  finalizedAt: string;
}> {
  const response = await apiClient.post(`/conclusions/${requestId}/finalize`, { finalRemark });
  return response.data.data;
}

/**
 * Get conclusion for a request
 * Returns null if conclusion doesn't exist (404) instead of throwing error
 */
export async function getConclusion(requestId: string): Promise<ConclusionRemark | null> {
  try {
    const response = await apiClient.get(`/conclusions/${requestId}`);
    return response.data.data;
  } catch (error: any) {
    // Handle 404 gracefully - conclusion doesn't exist yet, which is normal
    if (error.response?.status === 404) {
      return null;
    }
    // Re-throw other errors
    throw error;
  }
}

