/**
 * Chat Integration for Data Sheriff Agent
 * Provides middleware and helpers for integrating with the chat API
 */

import { dataSheriffAgentV2 as dataSheriffAgent, DataSheriffResponse } from './DataSheriffAgentV2';

export interface EnhancedChatRequest {
  originalMessage: string;
  enhancedMessage: string;
  dataSheriffContext: string;
  validationSummary: {
    confidence: number;
    warnings: string[];
    correctionsApplied: number;
    coverageScore: number;
  };
  shouldProceed: boolean;
  suggestedResponse?: string;
}

/**
 * Process a chat message through the Data Sheriff before sending to LLM
 */
export async function preprocessChatMessage(
  message: string,
  domain: string
): Promise<EnhancedChatRequest> {
  // Only process sports domain queries
  if (domain !== 'sports') {
    return {
      originalMessage: message,
      enhancedMessage: message,
      dataSheriffContext: '',
      validationSummary: {
        confidence: 1,
        warnings: [],
        correctionsApplied: 0,
        coverageScore: 1,
      },
      shouldProceed: true,
    };
  }

  try {
    const result = await dataSheriffAgent.processQuery(message);

    // Check if we should show a clarification response instead
    const ambiguities = result.validation.warnings.filter(
      w => w.includes('could refer to') || w.includes('could mean')
    );

    let suggestedResponse: string | undefined;

    // If confidence is low OR there are ambiguity warnings, suggest clarification
    // Threshold raised to 0.5 to catch more ambiguous cases
    const hasAmbiguityWarning = result.validation.warnings.some(
      w => w.includes('could refer to') || w.includes('could mean') || w.includes('Please specify')
    );
    if (result.metadata.confidenceScore < 0.5 || hasAmbiguityWarning) {
      suggestedResponse = generateClarificationResponse(result);
    }

    return {
      originalMessage: message,
      enhancedMessage: result.enhancedQuery,
      dataSheriffContext: result.contextForLLM,
      validationSummary: {
        confidence: result.metadata.confidenceScore,
        warnings: result.validation.warnings,
        correctionsApplied: result.metadata.correctionsApplied,
        coverageScore: result.metadata.coverageScore,
      },
      shouldProceed: result.success && result.metadata.confidenceScore >= 0.3,
      suggestedResponse,
    };
  } catch (error) {
    console.error('Data Sheriff preprocessing failed:', error);
    // Fail gracefully - allow the original message through
    return {
      originalMessage: message,
      enhancedMessage: message,
      dataSheriffContext: '',
      validationSummary: {
        confidence: 0.5,
        warnings: ['Data validation temporarily unavailable'],
        correctionsApplied: 0,
        coverageScore: 0.5,
      },
      shouldProceed: true,
    };
  }
}

/**
 * Generate a clarification response when confidence is too low
 */
function generateClarificationResponse(result: DataSheriffResponse): string {
  const parts: string[] = [];

  parts.push("I need a bit more clarification to give you accurate information:\n");

  // Add ambiguity warnings
  const ambiguities = result.validation.warnings.filter(
    w => w.includes('could refer to') || w.includes('could mean') || w.includes('Could')
  );

  if (ambiguities.length > 0) {
    parts.push("**Clarifications needed:**");
    for (const ambiguity of ambiguities.slice(0, 3)) {
      parts.push(`- ${ambiguity}`);
    }
  }

  // Add data coverage note
  if (result.metadata.coverageScore < 0.5) {
    parts.push(`\n**Note:** Data coverage for this query is limited (${Math.round(result.metadata.coverageScore * 100)}%).`);

    for (const suggestion of result.validation.enrichedContext.coverage.suggestions.slice(0, 2)) {
      parts.push(`- ${suggestion}`);
    }
  }

  parts.push("\nCould you please provide more specific details?");

  return parts.join('\n');
}

/**
 * Build an enhanced system prompt with Data Sheriff context
 */
export function buildEnhancedSystemPrompt(
  basePrompt: string,
  dataSheriffContext: string
): string {
  if (!dataSheriffContext) {
    return basePrompt;
  }

  return `${basePrompt}

## Data Validation Context
The following information has been validated and prepared by the Data Sheriff agent.
Use this context to provide accurate, data-backed responses.

${dataSheriffContext}

IMPORTANT:
- Use the resolved entity names (not the raw user input)
- Note any warnings about data quality or coverage
- If coverage is low, mention this limitation in your response
- Use inferred data with caution and note when doing so
`;
}

/**
 * Post-process the LLM response to add data quality notes
 */
export function postprocessResponse(
  response: string,
  validationSummary: EnhancedChatRequest['validationSummary']
): string {
  // Add data quality footer if there are significant issues
  if (validationSummary.coverageScore < 0.7 || validationSummary.warnings.length > 0) {
    const notes: string[] = [];

    if (validationSummary.coverageScore < 0.7) {
      notes.push(`Data coverage: ${Math.round(validationSummary.coverageScore * 100)}%`);
    }

    if (validationSummary.correctionsApplied > 0) {
      notes.push(`${validationSummary.correctionsApplied} query correction(s) applied`);
    }

    // Add relevant warnings
    const relevantWarnings = validationSummary.warnings
      .filter(w => !w.includes('could refer to')) // Exclude ambiguity warnings
      .slice(0, 2);

    if (relevantWarnings.length > 0) {
      notes.push(...relevantWarnings);
    }

    if (notes.length > 0 && !response.includes("📊 **Data Notes:**")) {
      response += `\n\n---\n📊 **Data Notes:** ${notes.join(' | ')}`;
    }
  }

  return response;
}

/**
 * Quick validation check for common query issues
 */
export async function quickValidate(message: string): Promise<{
  hasIssues: boolean;
  issues: string[];
}> {
  const issues: string[] = [];

  // Check for common typos in player/team names
  const commonTypos: Record<string, string> = {
    'lebrone': 'LeBron',
    'lebrown': 'LeBron',
    'stefon': 'Steph',
    'giannis': 'Giannis', // Correct spelling reminder
    'antetokounpo': 'Antetokounmpo',
    'maholmes': 'Mahomes',
    'lakerss': 'Lakers',
    'celitcs': 'Celtics',
  };

  const lowerMessage = message.toLowerCase();
  for (const [typo, correct] of Object.entries(commonTypos)) {
    if (lowerMessage.includes(typo)) {
      issues.push(`Did you mean "${correct}" instead of "${typo}"?`);
    }
  }

  // Check for ambiguous abbreviations without context
  const ambiguousTerms = [
    { term: /\bla\b/i, note: '"LA" - Lakers, Clippers, Dodgers, Angels, Rams, or Chargers?' },
    { term: /\bny\b/i, note: '"NY" - Knicks, Nets, Yankees, Mets, Giants, or Jets?' },
    { term: /\bchi\b/i, note: '"Chi" - Bulls, Cubs, Bears, or White Sox?' },
  ];

  for (const { term, note } of ambiguousTerms) {
    if (term.test(message)) {
      // Check if there's more context to disambiguate
      const hasContext = /\b(nba|nfl|mlb|nhl|basketball|football|baseball|hockey)\b/i.test(message);
      if (!hasContext) {
        issues.push(note);
      }
    }
  }

  return {
    hasIssues: issues.length > 0,
    issues,
  };
}

/**
 * Format Data Sheriff response for logging/debugging
 */
export function formatDebugLog(result: DataSheriffResponse): string {
  return [
    '=== DATA SHERIFF DEBUG ===',
    `Query: "${result.deconstructedQuery.originalQuery}"`,
    `Enhanced: "${result.enhancedQuery}"`,
    `Intent: ${result.deconstructedQuery.intent.primary} (${Math.round(result.deconstructedQuery.intent.confidence * 100)}%)`,
    `Entities: ${result.metadata.entitiesResolved}`,
    `Corrections: ${result.metadata.correctionsApplied}`,
    `Coverage: ${Math.round(result.metadata.coverageScore * 100)}%`,
    `Confidence: ${Math.round(result.metadata.confidenceScore * 100)}%`,
    `Time: ${result.processingTimeMs}ms`,
    result.validation.warnings.length > 0
      ? `Warnings: ${result.validation.warnings.join('; ')}`
      : 'Warnings: None',
    '========================',
  ].join('\n');
}
