/**
 * AI-Powered Filtering Service
 *
 * Runs automated filtering rules against projects:
 * - Field-based rules (age checks, category, country, etc.)
 * - Document checks (file existence/types)
 * - AI screening (GPT interprets criteria text, flags spam)
 *
 * GDPR Compliance:
 * - All project data is anonymized before AI processing
 * - Only necessary fields sent to OpenAI
 * - No personal identifiers in prompts or responses
 */

import { getOpenAI, getConfiguredModel, buildCompletionParams } from '@/lib/openai'
import { logAIUsage, extractTokenUsage } from '@/server/utils/ai-usage'
import { classifyAIError, createParseError, logAIError } from './ai-errors'
import {
  anonymizeProjectsForAI,
  validateAnonymizedProjects,
  type ProjectWithRelations,
  type AnonymizedProjectForAI,
  type ProjectAIMapping,
} from './anonymization'
import type { Prisma, FileType, SubmissionSource, PrismaClient } from '@prisma/client'

// ─── Progress Callback Type ─────────────────────────────────────────────────

export type ProgressCallback = (progress: {
  currentBatch: number
  totalBatches: number
  processedCount: number
  tokensUsed?: number
}) => Promise<void>

// ─── Types ──────────────────────────────────────────────────────────────────

export type FieldRuleCondition = {
  field:
    | 'competitionCategory'
    | 'foundedAt'
    | 'country'
    | 'geographicZone'
    | 'tags'
    | 'oceanIssue'
  operator:
    | 'equals'
    | 'not_equals'
    | 'greater_than'
    | 'less_than'
    | 'contains'
    | 'in'
    | 'not_in'
    | 'older_than_years'
    | 'newer_than_years'
    | 'is_empty'
  value: string | number | string[]
}

export type FieldRuleConfig = {
  conditions: FieldRuleCondition[]
  logic: 'AND' | 'OR'
  action: 'PASS' | 'REJECT' | 'FLAG'
}

export type DocumentCheckConfig = {
  requiredFileTypes?: string[] // e.g. ['pdf', 'docx']
  minFileCount?: number
  action: 'PASS' | 'REJECT' | 'FLAG'
}

export type AIScreeningConfig = {
  criteriaText: string
  action: 'FLAG' // AI screening always flags for human review
  // Performance settings
  batchSize?: number // Projects per API call (1-50, default 20)
  parallelBatches?: number // Concurrent API calls (1-10, default 1)
}

export type RuleConfig = FieldRuleConfig | DocumentCheckConfig | AIScreeningConfig

export interface RuleResult {
  ruleId: string
  ruleName: string
  ruleType: string
  passed: boolean
  action: 'PASS' | 'REJECT' | 'FLAG'
  reasoning?: string
}

export interface ProjectFilteringResult {
  projectId: string
  outcome: 'PASSED' | 'FILTERED_OUT' | 'FLAGGED'
  ruleResults: RuleResult[]
  aiScreeningJson?: Record<string, unknown>
}

interface ProjectForFiltering {
  id: string
  title: string
  description?: string | null
  competitionCategory?: string | null
  foundedAt?: Date | null
  country?: string | null
  geographicZone?: string | null
  tags: string[]
  oceanIssue?: string | null
  wantsMentorship?: boolean | null
  institution?: string | null
  submissionSource?: SubmissionSource
  submittedAt?: Date | null
  files: Array<{ id: string; fileName: string; fileType?: FileType | null }>
  _count?: {
    teamMembers?: number
    files?: number
  }
}

interface FilteringRuleInput {
  id: string
  name: string
  ruleType: string
  configJson: Prisma.JsonValue
  priority: number
  isActive: boolean
}

// ─── Constants ───────────────────────────────────────────────────────────────

const DEFAULT_BATCH_SIZE = 20
const MAX_BATCH_SIZE = 50
const MIN_BATCH_SIZE = 1
const DEFAULT_PARALLEL_BATCHES = 1
const MAX_PARALLEL_BATCHES = 10

// Optimized system prompt (compressed for token efficiency)
const AI_SCREENING_SYSTEM_PROMPT = `Project screening assistant. Evaluate against criteria, return JSON.
Format: {"projects": [{project_id, meets_criteria: bool, confidence: 0-1, reasoning: str, quality_score: 1-10, spam_risk: bool}]}
Be objective. Base evaluation only on provided data. No personal identifiers in reasoning.`

// ─── Field-Based Rule Evaluation ────────────────────────────────────────────

function evaluateCondition(
  condition: FieldRuleCondition,
  project: ProjectForFiltering
): boolean {
  const { field, operator, value } = condition

  // Get field value from project
  let fieldValue: unknown
  switch (field) {
    case 'competitionCategory':
      fieldValue = project.competitionCategory
      break
    case 'foundedAt':
      fieldValue = project.foundedAt
      break
    case 'country':
      fieldValue = project.country
      break
    case 'geographicZone':
      fieldValue = project.geographicZone
      break
    case 'tags':
      fieldValue = project.tags
      break
    case 'oceanIssue':
      fieldValue = project.oceanIssue
      break
    default:
      return false
  }

  switch (operator) {
    case 'equals':
      return String(fieldValue) === String(value)
    case 'not_equals':
      return String(fieldValue) !== String(value)
    case 'contains':
      if (Array.isArray(fieldValue)) {
        return fieldValue.some((v) =>
          String(v).toLowerCase().includes(String(value).toLowerCase())
        )
      }
      return String(fieldValue || '')
        .toLowerCase()
        .includes(String(value).toLowerCase())
    case 'in':
      if (Array.isArray(value)) {
        return value.includes(String(fieldValue))
      }
      return false
    case 'not_in':
      if (Array.isArray(value)) {
        return !value.includes(String(fieldValue))
      }
      return true
    case 'is_empty':
      if (fieldValue === null || fieldValue === undefined) return true
      if (Array.isArray(fieldValue)) return fieldValue.length === 0
      return String(fieldValue).trim() === ''
    case 'older_than_years': {
      if (!fieldValue || !(fieldValue instanceof Date)) return false
      const yearsAgo = new Date()
      yearsAgo.setFullYear(yearsAgo.getFullYear() - Number(value))
      return fieldValue < yearsAgo
    }
    case 'newer_than_years': {
      if (!fieldValue || !(fieldValue instanceof Date)) return false
      const yearsAgo = new Date()
      yearsAgo.setFullYear(yearsAgo.getFullYear() - Number(value))
      return fieldValue >= yearsAgo
    }
    case 'greater_than':
      return Number(fieldValue) > Number(value)
    case 'less_than':
      return Number(fieldValue) < Number(value)
    default:
      return false
  }
}

export function evaluateFieldRule(
  config: FieldRuleConfig,
  project: ProjectForFiltering
): { passed: boolean; action: 'PASS' | 'REJECT' | 'FLAG' } {
  const results = config.conditions.map((c) => evaluateCondition(c, project))

  const allConditionsMet =
    config.logic === 'AND'
      ? results.every(Boolean)
      : results.some(Boolean)

  if (config.action === 'PASS') {
    return { passed: allConditionsMet, action: config.action }
  }
  return { passed: !allConditionsMet, action: config.action }
}

// ─── Document Check Evaluation ──────────────────────────────────────────────

export function evaluateDocumentRule(
  config: DocumentCheckConfig,
  project: ProjectForFiltering
): { passed: boolean; action: 'PASS' | 'REJECT' | 'FLAG' } {
  const files = project.files || []

  if (config.minFileCount !== undefined && files.length < config.minFileCount) {
    return { passed: false, action: config.action }
  }

  if (config.requiredFileTypes && config.requiredFileTypes.length > 0) {
    const fileExtensions = files.map((f) => {
      const ext = f.fileName.split('.').pop()?.toLowerCase()
      return ext || ''
    })
    const hasAllTypes = config.requiredFileTypes.every((type) =>
      fileExtensions.some((ext) => ext === type.toLowerCase())
    )
    if (!hasAllTypes) {
      return { passed: false, action: config.action }
    }
  }

  return { passed: true, action: config.action }
}

// ─── AI Screening ───────────────────────────────────────────────────────────

interface AIScreeningResult {
  meetsCriteria: boolean
  confidence: number
  reasoning: string
  qualityScore: number
  spamRisk: boolean
}

/**
 * Convert project to enhanced format for anonymization
 */
function toProjectWithRelations(project: ProjectForFiltering): ProjectWithRelations {
  return {
    id: project.id,
    title: project.title,
    description: project.description,
    competitionCategory: project.competitionCategory as any,
    oceanIssue: project.oceanIssue as any,
    country: project.country,
    geographicZone: project.geographicZone,
    institution: project.institution,
    tags: project.tags,
    foundedAt: project.foundedAt,
    wantsMentorship: project.wantsMentorship ?? false,
    submissionSource: project.submissionSource ?? 'MANUAL',
    submittedAt: project.submittedAt,
    _count: {
      teamMembers: project._count?.teamMembers ?? 0,
      files: project.files?.length ?? 0,
    },
    files: project.files?.map(f => ({ fileType: f.fileType ?? null })) ?? [],
  }
}

/**
 * Execute AI screening on a batch of projects
 */
async function processAIBatch(
  openai: NonNullable<Awaited<ReturnType<typeof getOpenAI>>>,
  model: string,
  criteriaText: string,
  anonymized: AnonymizedProjectForAI[],
  mappings: ProjectAIMapping[],
  userId?: string,
  entityId?: string
): Promise<{
  results: Map<string, AIScreeningResult>
  tokensUsed: number
}> {
  const results = new Map<string, AIScreeningResult>()
  let tokensUsed = 0

  // Build optimized prompt
  const userPrompt = `CRITERIA: ${criteriaText}
PROJECTS: ${JSON.stringify(anonymized)}
Evaluate and return JSON.`

  try {
    const params = buildCompletionParams(model, {
      messages: [
        { role: 'system', content: AI_SCREENING_SYSTEM_PROMPT },
        { role: 'user', content: userPrompt },
      ],
      jsonMode: true,
      temperature: 0.3,
      maxTokens: 4000,
    })

    const response = await openai.chat.completions.create(params)
    const usage = extractTokenUsage(response)
    tokensUsed = usage.totalTokens

    // Log usage
    await logAIUsage({
      userId,
      action: 'FILTERING',
      entityType: 'Round',
      entityId,
      model,
      promptTokens: usage.promptTokens,
      completionTokens: usage.completionTokens,
      totalTokens: usage.totalTokens,
      batchSize: anonymized.length,
      itemsProcessed: anonymized.length,
      status: 'SUCCESS',
    })

    const content = response.choices[0]?.message?.content
    if (!content) {
      throw new Error('Empty response from AI')
    }

    const parsed = JSON.parse(content) as {
      projects: Array<{
        project_id: string
        meets_criteria: boolean
        confidence: number
        reasoning: string
        quality_score: number
        spam_risk: boolean
      }>
    }

    // Map results back to real IDs
    for (const result of parsed.projects || []) {
      const mapping = mappings.find((m) => m.anonymousId === result.project_id)
      if (mapping) {
        results.set(mapping.realId, {
          meetsCriteria: result.meets_criteria,
          confidence: result.confidence,
          reasoning: result.reasoning,
          qualityScore: result.quality_score,
          spamRisk: result.spam_risk,
        })
      }
    }
  } catch (error) {
    // Check if parse error
    if (error instanceof SyntaxError) {
      const parseError = createParseError(error.message)
      logAIError('Filtering', 'batch processing', parseError)

      await logAIUsage({
        userId,
        action: 'FILTERING',
        entityType: 'Round',
        entityId,
        model,
        promptTokens: 0,
        completionTokens: 0,
        totalTokens: tokensUsed,
        batchSize: anonymized.length,
        itemsProcessed: 0,
        status: 'ERROR',
        errorMessage: parseError.message,
      })

      // Flag all for manual review
      for (const mapping of mappings) {
        results.set(mapping.realId, {
          meetsCriteria: false,
          confidence: 0,
          reasoning: 'AI response parse error — flagged for manual review',
          qualityScore: 5,
          spamRisk: false,
        })
      }
    } else {
      throw error // Re-throw for outer catch
    }
  }

  return { results, tokensUsed }
}

export async function executeAIScreening(
  config: AIScreeningConfig,
  projects: ProjectForFiltering[],
  userId?: string,
  entityId?: string,
  onProgress?: ProgressCallback
): Promise<Map<string, AIScreeningResult>> {
  const results = new Map<string, AIScreeningResult>()

  try {
    const openai = await getOpenAI()
    if (!openai) {
      console.warn('[AI Filtering] OpenAI not configured')
      for (const p of projects) {
        results.set(p.id, {
          meetsCriteria: false,
          confidence: 0,
          reasoning: 'AI screening unavailable — flagged for manual review',
          qualityScore: 5,
          spamRisk: false,
        })
      }
      return results
    }

    const model = await getConfiguredModel()

    // Get batch settings from config
    const batchSize = Math.min(
      MAX_BATCH_SIZE,
      Math.max(MIN_BATCH_SIZE, config.batchSize ?? DEFAULT_BATCH_SIZE)
    )
    const parallelBatches = Math.min(
      MAX_PARALLEL_BATCHES,
      Math.max(1, config.parallelBatches ?? DEFAULT_PARALLEL_BATCHES)
    )

    console.log(`[AI Filtering] Using model: ${model} for ${projects.length} projects (batch size: ${batchSize}, parallel: ${parallelBatches})`)

    // Convert and anonymize projects
    const projectsWithRelations = projects.map(toProjectWithRelations)
    const { anonymized, mappings } = anonymizeProjectsForAI(projectsWithRelations, 'FILTERING')

    // Validate anonymization
    if (!validateAnonymizedProjects(anonymized)) {
      console.error('[AI Filtering] Anonymization validation failed')
      throw new Error('GDPR compliance check failed: PII detected in anonymized data')
    }

    let totalTokens = 0
    const totalBatches = Math.ceil(anonymized.length / batchSize)
    let processedBatches = 0

    // Create batch chunks for parallel processing
    const batches: Array<{ anon: typeof anonymized; maps: typeof mappings; index: number }> = []
    for (let i = 0; i < anonymized.length; i += batchSize) {
      batches.push({
        anon: anonymized.slice(i, i + batchSize),
        maps: mappings.slice(i, i + batchSize),
        index: batches.length,
      })
    }

    // Process batches in parallel chunks
    for (let i = 0; i < batches.length; i += parallelBatches) {
      const parallelChunk = batches.slice(i, i + parallelBatches)

      console.log(`[AI Filtering] Processing batches ${i + 1}-${Math.min(i + parallelBatches, batches.length)} of ${totalBatches} (${parallelChunk.length} in parallel)`)

      // Run parallel batches concurrently
      const batchPromises = parallelChunk.map(async (batch) => {
        const { results: batchResults, tokensUsed } = await processAIBatch(
          openai,
          model,
          config.criteriaText,
          batch.anon,
          batch.maps,
          userId,
          entityId
        )
        return { batchResults, tokensUsed, index: batch.index }
      })

      const parallelResults = await Promise.all(batchPromises)

      // Merge results from all parallel batches
      for (const { batchResults, tokensUsed } of parallelResults) {
        totalTokens += tokensUsed
        for (const [id, result] of batchResults) {
          results.set(id, result)
        }
        processedBatches++
      }

      // Report progress after each parallel chunk
      if (onProgress) {
        await onProgress({
          currentBatch: processedBatches,
          totalBatches,
          processedCount: Math.min(processedBatches * batchSize, anonymized.length),
          tokensUsed: totalTokens,
        })
      }
    }

    console.log(`[AI Filtering] Completed. Total tokens: ${totalTokens}`)

  } catch (error) {
    const classified = classifyAIError(error)
    logAIError('Filtering', 'executeAIScreening', classified)

    // Log failed attempt
    await logAIUsage({
      userId,
      action: 'FILTERING',
      entityType: 'Round',
      entityId,
      model: 'unknown',
      promptTokens: 0,
      completionTokens: 0,
      totalTokens: 0,
      batchSize: projects.length,
      itemsProcessed: 0,
      status: 'ERROR',
      errorMessage: classified.message,
    })

    // Flag all for manual review with error info
    for (const p of projects) {
      results.set(p.id, {
        meetsCriteria: false,
        confidence: 0,
        reasoning: `AI screening error: ${classified.message}`,
        qualityScore: 5,
        spamRisk: false,
      })
    }
  }

  return results
}

// ─── Main Execution ─────────────────────────────────────────────────────────

export async function executeFilteringRules(
  rules: FilteringRuleInput[],
  projects: ProjectForFiltering[],
  userId?: string,
  roundId?: string,
  onProgress?: ProgressCallback
): Promise<ProjectFilteringResult[]> {
  const activeRules = rules
    .filter((r) => r.isActive)
    .sort((a, b) => a.priority - b.priority)

  // Separate AI screening rules (need batch processing)
  const aiRules = activeRules.filter((r) => r.ruleType === 'AI_SCREENING')
  const nonAiRules = activeRules.filter((r) => r.ruleType !== 'AI_SCREENING')

  // Pre-compute AI screening results if needed
  const aiResults = new Map<string, Map<string, AIScreeningResult>>()

  for (const aiRule of aiRules) {
    const config = aiRule.configJson as unknown as AIScreeningConfig
    const screeningResults = await executeAIScreening(config, projects, userId, roundId, onProgress)
    aiResults.set(aiRule.id, screeningResults)
  }

  // Evaluate each project
  const results: ProjectFilteringResult[] = []

  for (const project of projects) {
    const ruleResults: RuleResult[] = []
    let hasFailed = false
    let hasFlagged = false

    // Evaluate non-AI rules
    for (const rule of nonAiRules) {
      let result: { passed: boolean; action: 'PASS' | 'REJECT' | 'FLAG' }

      if (rule.ruleType === 'FIELD_BASED') {
        const config = rule.configJson as unknown as FieldRuleConfig
        result = evaluateFieldRule(config, project)
      } else if (rule.ruleType === 'DOCUMENT_CHECK') {
        const config = rule.configJson as unknown as DocumentCheckConfig
        result = evaluateDocumentRule(config, project)
      } else {
        continue
      }

      ruleResults.push({
        ruleId: rule.id,
        ruleName: rule.name,
        ruleType: rule.ruleType,
        passed: result.passed,
        action: result.action,
      })

      if (!result.passed) {
        if (result.action === 'REJECT') hasFailed = true
        if (result.action === 'FLAG') hasFlagged = true
      }
    }

    // Evaluate AI rules
    for (const aiRule of aiRules) {
      const ruleScreening = aiResults.get(aiRule.id)
      const screening = ruleScreening?.get(project.id)

      if (screening) {
        const passed = screening.meetsCriteria && !screening.spamRisk
        ruleResults.push({
          ruleId: aiRule.id,
          ruleName: aiRule.name,
          ruleType: 'AI_SCREENING',
          passed,
          action: 'FLAG',
          reasoning: screening.reasoning,
        })

        if (!passed) hasFlagged = true
      }
    }

    // Determine overall outcome
    let outcome: 'PASSED' | 'FILTERED_OUT' | 'FLAGGED'
    if (hasFailed) {
      outcome = 'FILTERED_OUT'
    } else if (hasFlagged) {
      outcome = 'FLAGGED'
    } else {
      outcome = 'PASSED'
    }

    // Collect AI screening data
    const aiScreeningData: Record<string, unknown> = {}
    for (const aiRule of aiRules) {
      const screening = aiResults.get(aiRule.id)?.get(project.id)
      if (screening) {
        aiScreeningData[aiRule.id] = screening
      }
    }

    results.push({
      projectId: project.id,
      outcome,
      ruleResults,
      aiScreeningJson:
        Object.keys(aiScreeningData).length > 0 ? aiScreeningData : undefined,
    })
  }

  return results
}
