import { apiRequest, buildUrl, type PaginatedResponse } from './base';
import type { ReviewResult, RuleCheckResult, AIAnalysis } from '~/models/review';

/**
 * 评查结果API服务
 */

interface ReviewFilterParams {
  fileId?: string;
  reviewStatus?: string;
  startDate?: string;
  endDate?: string;
  keyword?: string;
  page?: number;
  pageSize?: number;
}

// 获取评查结果列表
export async function getReviews(params?: ReviewFilterParams): Promise<PaginatedResponse<ReviewResult>> {
  const url = buildUrl('/api/reviews', params);
  return apiRequest<PaginatedResponse<ReviewResult>>(url);
}

// 获取单个评查结果
export async function getReview(id: string): Promise<ReviewResult> {
  const url = buildUrl(`/api/reviews/${id}`);
  return apiRequest<ReviewResult>(url);
}

// 获取评查点结果列表
export async function getReviewPoints(reviewId: string): Promise<RuleCheckResult[]> {
  const url = buildUrl(`/api/reviews/${reviewId}/points`);
  return apiRequest<RuleCheckResult[]>(url);
}

// 开始评查
export async function startReview(fileId: string): Promise<ReviewResult> {
  const url = buildUrl(`/api/reviews/start/${fileId}`);
  return apiRequest<ReviewResult>(url, 'POST');
}

// 更新评查点结果
export async function updateReviewPoint(reviewId: string, pointId: string, data: Partial<RuleCheckResult>): Promise<RuleCheckResult> {
  const url = buildUrl(`/api/reviews/${reviewId}/points/${pointId}`);
  return apiRequest<RuleCheckResult>(url, 'PUT', data);
}

// 完成评查
export async function completeReview(reviewId: string): Promise<ReviewResult> {
  const url = buildUrl(`/api/reviews/${reviewId}/complete`);
  return apiRequest<ReviewResult>(url, 'POST');
}

// 获取AI分析
export async function getAIAnalysis(reviewId: string): Promise<AIAnalysis> {
  const url = buildUrl(`/api/reviews/${reviewId}/analysis`);
  return apiRequest<AIAnalysis>(url);
}

// 导出评查报告
export async function exportReviewReport(reviewId: string, format: 'pdf' | 'word' = 'pdf'): Promise<Blob> {
  const url = buildUrl(`/api/reviews/${reviewId}/export`, { format });
  
  const response = await fetch(url, {
    method: 'GET',
    headers: {
      'Accept': format === 'pdf' ? 'application/pdf' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    }
  });
  
  if (!response.ok) {
    throw new Error('导出评查报告失败');
  }
  
  return response.blob();
} 