import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export interface AILearningStatus {
  phase: 'INITIAL' | 'PATTERN_RECOGNITION' | 'ADVANCED' | 'EXPERT'
  phaseDescription: string
  totalAnalyses: number
  totalTrades: number
  avgAccuracy: number
  winRate: number
  confidenceLevel: number
  daysActive: number
  nextMilestone: string
  strengths: string[]
  improvements: string[]
  recommendation: string
}

export async function getAILearningStatus(userId: string): Promise<AILearningStatus> {
  try {
    // Get learning data
    const learningData = await prisma.aILearningData.findMany({
      where: { userId },
      orderBy: { createdAt: 'desc' }
    })

    // Get trade data - use real database data instead of demo numbers
    const trades = await prisma.trade.findMany({
      where: { 
        userId,
        // isAutomated: true  // This field might not exist in current schema
      },
      orderBy: { createdAt: 'desc' }
    })

    // Calculate real trade statistics from database
    const displayedTrades = trades.length
    const completedTrades = trades.filter(t => t.status === 'COMPLETED')
    const winningTrades = completedTrades.filter(t => (t.profit || 0) > 0)

    // Calculate metrics from real trade data
    const totalAnalyses = learningData.length
    const totalTrades = displayedTrades
    const winRate = completedTrades.length > 0 ? (winningTrades.length / completedTrades.length) : 0
    
    // Calculate average accuracy based on actual win rate and trade performance
    let avgAccuracy = winRate // Use actual win rate as accuracy baseline
    if (totalAnalyses > 0 && winRate > 0) {
      // Enhance accuracy based on consistency: more analyses with good performance = higher accuracy
      const consistencyBonus = Math.min(totalAnalyses / 200, 0.15) // Up to 15% bonus for experience
      avgAccuracy = Math.min(winRate + consistencyBonus, 0.95) // Cap at 95%
    } else if (totalAnalyses > 0) {
      // If no wins yet, base accuracy on analysis experience only
      avgAccuracy = Math.min(0.40 + (totalAnalyses * 0.002), 0.60) // Start low, cap at 60% without wins
    }
    
    // Calculate confidence based on actual trading performance
    let avgConfidence = 50 // Start at 50%
    if (completedTrades.length > 0) {
      // Base confidence on win rate and number of trades
      const winRateConfidence = winRate * 70 // Win rate contributes up to 70%
      const experienceBonus = Math.min(completedTrades.length * 2, 30) // Up to 30% for experience
      avgConfidence = Math.min(winRateConfidence + experienceBonus, 95) // Cap at 95%
    } else if (totalAnalyses > 0) {
      // If no completed trades, base on analysis experience only
      avgConfidence = Math.min(50 + (totalAnalyses * 0.5), 70) // Cap at 70% without trade results
    }

    // Calculate days active
    const firstAnalysis = learningData[learningData.length - 1]
    const daysActive = firstAnalysis 
      ? Math.ceil((Date.now() - new Date(firstAnalysis.createdAt).getTime()) / (1000 * 60 * 60 * 24))
      : 0

    // Determine learning phase based on actual performance data
    let phase: AILearningStatus['phase'] = 'INITIAL'
    let phaseDescription = 'Learning market basics'
    let nextMilestone = 'Complete 10 trades to advance'
    
    if (completedTrades.length >= 50 && winRate >= 0.75 && avgAccuracy >= 0.75) {
      phase = 'EXPERT'
      phaseDescription = 'Expert-level performance'
      nextMilestone = 'Maintain excellence'
    } else if (completedTrades.length >= 20 && winRate >= 0.65 && avgAccuracy >= 0.65) {
      phase = 'ADVANCED'
      phaseDescription = 'Advanced pattern mastery'
      nextMilestone = 'Achieve 75% win rate for expert level'
    } else if (completedTrades.length >= 10 && winRate >= 0.55) {
      phase = 'PATTERN_RECOGNITION'
      phaseDescription = 'Recognizing patterns'
      nextMilestone = 'Reach 65% win rate for advanced level'
    } else if (completedTrades.length >= 5) {
      phase = 'PATTERN_RECOGNITION'
      phaseDescription = 'Building trading experience'
      nextMilestone = 'Reach 55% win rate with 10+ trades'
    }

    // Determine strengths and improvements based on real performance
    const strengths: string[] = []
    const improvements: string[] = []
    
    if (avgConfidence > 70) strengths.push('High confidence in analysis')
    if (winRate > 0.6) strengths.push('Good trade selection')
    if (avgAccuracy > 0.6) strengths.push('Accurate predictions')
    if (totalAnalyses > 50) strengths.push('Rich learning dataset')
    if (completedTrades.length > 10) strengths.push('Active trading experience')
    
    if (avgConfidence < 60) improvements.push('Build confidence through experience')
    if (winRate < 0.6) improvements.push('Improve trade selection criteria')
    if (avgAccuracy < 0.6) improvements.push('Enhance prediction accuracy')
    if (totalAnalyses < 50) improvements.push('Gather more analysis data')
    if (completedTrades.length < 10) improvements.push('Complete more trades for better statistics')

    // Generate recommendation
    let recommendation = 'Continue collecting data'
    if (phase === 'EXPERT') {
      recommendation = 'AI is performing at expert level - ready for increased position sizes'
    } else if (phase === 'ADVANCED') {
      recommendation = 'AI shows strong performance - consider gradual position size increases'
    } else if (phase === 'PATTERN_RECOGNITION') {
      recommendation = 'AI is learning patterns - maintain conservative position sizes'
    } else {
      recommendation = 'AI is in initial learning phase - use minimum position sizes'
    }

    return {
      phase,
      phaseDescription,
      totalAnalyses,
      totalTrades,
      avgAccuracy,
      winRate,
      confidenceLevel: avgConfidence,
      daysActive,
      nextMilestone,
      strengths: strengths.length > 0 ? strengths : ['Building initial experience'],
      improvements: improvements.length > 0 ? improvements : ['Continue learning process'],
      recommendation
    }

  } catch (error) {
    console.error('Error getting AI learning status:', error)
    
    // Return default status if error
    return {
      phase: 'INITIAL',
      phaseDescription: 'Learning market basics',
      totalAnalyses: 0,
      totalTrades: 0,
      avgAccuracy: 0,
      winRate: 0,
      confidenceLevel: 0,
      daysActive: 0,
      nextMilestone: 'Start automation to begin learning',
      strengths: ['Ready to learn'],
      improvements: ['Begin collecting data'],
      recommendation: 'Start automation to begin AI learning process'
    }
  }
}
