/**
 * Neutralization API
 *
 * API functions for the Neutralization feature.
 * Endpoints use /api/neutralization/*. Context headers (X-Mandate-Id, X-Instance-Id)
 * are set automatically by the api interceptor when on a feature instance page.
 */

import api from '../api';

// ============================================================================
// TYPES
// ============================================================================

export interface NeutralizationConfig {
  id?: string;
  mandateId: string;
  featureInstanceId: string;
  userId: string;
  enabled: boolean;
  namesToParse: string;
  sharepointSourcePath: string;
  sharepointTargetPath: string;
}

export interface NeutralizationResult {
  neutralized_text?: string;
  neutralized_bytes?: string | Uint8Array;
  neutralized_file_base64?: string;
  neutralized_file_name?: string;
  mime_type?: string;
  original_file_id?: string;
  neutralized_file_id?: string;
  mapping?: Record<string, string>;
  attributes?: Array<{
    id: string;
    originalText: string;
    patternType: string;
    fileId?: string;
  }>;
  processed_info?: Record<string, unknown>;
}

export interface NeutralizationAttribute {
  id: string;
  mandateId: string;
  featureInstanceId: string;
  userId: string;
  originalText: string;
  fileId?: string;
  patternType: string;
}

// ============================================================================
// API FUNCTIONS
// ============================================================================

export async function getNeutralizationConfig(): Promise<NeutralizationConfig> {
  const { data } = await api.get<NeutralizationConfig>('/api/neutralization/config');
  return data;
}

export async function saveNeutralizationConfig(
  configData: Partial<NeutralizationConfig> & { enabled: boolean; namesToParse: string; sharepointSourcePath: string; sharepointTargetPath: string }
): Promise<NeutralizationConfig> {
  const { data } = await api.post<NeutralizationConfig>('/api/neutralization/config', configData);
  return data;
}

export async function neutralizeText(text: string, fileId?: string): Promise<NeutralizationResult> {
  const { data } = await api.post<NeutralizationResult>('/api/neutralization/neutralize-text', {
    text,
    ...(fileId && { fileId }),
  });
  return data;
}

export async function resolveText(text: string): Promise<{ resolved_text: string }> {
  const { data } = await api.post<{ resolved_text: string }>('/api/neutralization/resolve-text', {
    text,
  });
  return data;
}

export async function getNeutralizationAttributes(fileId?: string): Promise<NeutralizationAttribute[]> {
  const params = fileId ? { fileId } : {};
  const { data } = await api.get<NeutralizationAttribute[]>('/api/neutralization/attributes', { params });
  return data;
}

export async function neutralizeFile(file: File): Promise<NeutralizationResult> {
  const formData = new FormData();
  formData.append('file', file);
  // Do NOT set Content-Type - axios sets it with boundary for FormData
  const { data } = await api.post<NeutralizationResult>('/api/neutralization/neutralize-file', formData);
  return data;
}

export async function processSharepointFiles(
  sourcePath: string,
  targetPath: string
): Promise<{ success: boolean; message?: string; [key: string]: unknown }> {
  const { data } = await api.post('/api/neutralization/process-sharepoint', {
    sourcePath,
    targetPath,
  });
  return data;
}
