import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Between } from 'typeorm';
import {
  Activity,
  ActivityType,
} from '../../database/entities/activity.entity';
import { Child } from '../../database/entities/child.entity';

export interface SleepPattern {
  averageDuration: number; // in minutes
  averageBedtime: string; // HH:MM format
  averageWakeTime: string;
  nightWakings: number;
  napCount: number;
  consistency: number; // 0-1 score
  trend: 'improving' | 'stable' | 'declining';
}

export interface FeedingPattern {
  averageInterval: number; // in hours
  averageDuration: number; // in minutes
  totalFeedings: number;
  feedingMethod: Record<string, number>; // e.g., { bottle: 5, nursing: 3 }
  consistency: number;
  trend: 'increasing' | 'stable' | 'decreasing';
}

export interface DiaperPattern {
  wetDiapersPerDay: number;
  dirtyDiapersPerDay: number;
  averageInterval: number; // in hours
  isHealthy: boolean;
  notes: string;
}

export interface GrowthSpurtDetection {
  isLikelyGrowthSpurt: boolean;
  confidence: number; // 0-1
  indicators: string[];
  expectedDuration: string; // e.g., "2-3 days"
  recommendations: string[];
}

export interface PatternInsights {
  sleep: SleepPattern | null;
  feeding: FeedingPattern | null;
  diaper: DiaperPattern | null;
  growthSpurt: GrowthSpurtDetection | null;
  recommendations: string[];
  concernsDetected: string[];
}

@Injectable()
export class PatternAnalysisService {
  private readonly logger = new Logger('PatternAnalysisService');

  constructor(
    @InjectRepository(Activity)
    private activityRepository: Repository<Activity>,
    @InjectRepository(Child)
    private childRepository: Repository<Child>,
  ) {}

  /**
   * Analyze all patterns for a child
   */
  async analyzePatterns(
    childId: string,
    days: number = 7,
  ): Promise<PatternInsights> {
    const cutoffDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000);

    const activities = await this.activityRepository.find({
      where: {
        childId,
        startedAt: Between(cutoffDate, new Date()),
      },
      order: { startedAt: 'ASC' },
    });

    const child = await this.childRepository.findOne({
      where: { id: childId },
    });

    if (!child) {
      throw new Error('Child not found');
    }

    // Analyze patterns
    const sleepPattern = await this.analyzeSleepPatterns(activities, child);
    const feedingPattern = await this.analyzeFeedingPatterns(activities, child);
    const diaperPattern = await this.analyzeDiaperPatterns(activities, child);
    const growthSpurt = await this.detectGrowthSpurt(
      activities,
      sleepPattern,
      feedingPattern,
      child,
    );

    // Generate recommendations and detect concerns
    const recommendations = this.generateRecommendations(
      sleepPattern,
      feedingPattern,
      diaperPattern,
      child,
    );
    const concernsDetected = this.detectConcerns(
      sleepPattern,
      feedingPattern,
      diaperPattern,
      child,
    );

    return {
      sleep: sleepPattern,
      feeding: feedingPattern,
      diaper: diaperPattern,
      growthSpurt,
      recommendations,
      concernsDetected,
    };
  }

  /**
   * Analyze sleep patterns
   */
  async analyzeSleepPatterns(
    activities: Activity[],
    child: Child,
  ): Promise<SleepPattern | null> {
    const sleepActivities = activities.filter(
      (a) => a.type === ActivityType.SLEEP && a.endedAt,
    );

    if (sleepActivities.length < 3) {
      return null;
    }

    // Calculate average duration
    const durations = sleepActivities.map(
      (a) => (a.endedAt!.getTime() - a.startedAt.getTime()) / (1000 * 60), // minutes
    );
    const averageDuration =
      durations.reduce((sum, d) => sum + d, 0) / durations.length;

    // Find bedtime pattern (sleep sessions starting between 6PM-11PM)
    const nightSleeps = sleepActivities.filter((a) => {
      const hour = a.startedAt.getHours();
      return hour >= 18 || hour <= 6;
    });

    const averageBedtime = this.calculateAverageTime(
      nightSleeps.map((a) => a.startedAt),
    );
    const averageWakeTime = this.calculateAverageTime(
      nightSleeps.map((a) => a.endedAt!),
    );

    // Count night wakings (sleep sessions during night hours)
    const nightWakings = nightSleeps.length > 0 ? nightSleeps.length - 1 : 0;

    // Count naps (sleep sessions during day hours)
    const naps = sleepActivities.filter((a) => {
      const hour = a.startedAt.getHours();
      return hour >= 7 && hour < 18;
    });
    const napCount = naps.length / (sleepActivities.length / 7); // average per day

    // Calculate consistency (standard deviation of sleep duration)
    const stdDev = this.calculateStdDev(durations);
    const consistency = Math.max(0, 1 - stdDev / averageDuration);

    // Determine trend
    const recentAvg = durations.slice(-3).reduce((a, b) => a + b, 0) / 3;
    const olderAvg =
      durations.slice(0, 3).reduce((a, b) => a + b, 0) /
      Math.min(3, durations.length);
    const trend =
      recentAvg > olderAvg * 1.1
        ? 'improving'
        : recentAvg < olderAvg * 0.9
          ? 'declining'
          : 'stable';

    return {
      averageDuration: Math.round(averageDuration),
      averageBedtime,
      averageWakeTime,
      nightWakings: Math.round(nightWakings),
      napCount: Math.round(napCount * 10) / 10,
      consistency: Math.round(consistency * 100) / 100,
      trend,
    };
  }

  /**
   * Analyze feeding patterns
   */
  async analyzeFeedingPatterns(
    activities: Activity[],
    child: Child,
  ): Promise<FeedingPattern | null> {
    const feedingActivities = activities.filter(
      (a) => a.type === ActivityType.FEEDING,
    );

    if (feedingActivities.length < 3) {
      return null;
    }

    // Calculate average interval between feedings
    const intervals: number[] = [];
    for (let i = 1; i < feedingActivities.length; i++) {
      const interval =
        (feedingActivities[i].startedAt.getTime() -
          feedingActivities[i - 1].startedAt.getTime()) /
        (1000 * 60 * 60); // hours
      intervals.push(interval);
    }
    const averageInterval =
      intervals.reduce((sum, i) => sum + i, 0) / intervals.length;

    // Calculate average duration (if available)
    const durationsInMinutes = feedingActivities
      .filter((a) => a.endedAt)
      .map((a) => (a.endedAt!.getTime() - a.startedAt.getTime()) / (1000 * 60));
    const averageDuration =
      durationsInMinutes.length > 0
        ? durationsInMinutes.reduce((sum, d) => sum + d, 0) /
          durationsInMinutes.length
        : 0;

    // Analyze feeding methods
    const feedingMethod: Record<string, number> = {};
    feedingActivities.forEach((a) => {
      const method = a.metadata?.method || 'unknown';
      feedingMethod[method] = (feedingMethod[method] || 0) + 1;
    });

    // Calculate consistency
    const stdDev = this.calculateStdDev(intervals);
    const consistency = Math.max(0, 1 - stdDev / averageInterval);

    // Determine trend
    const recentCount = feedingActivities.filter(
      (a) => a.startedAt.getTime() > Date.now() - 3 * 24 * 60 * 60 * 1000,
    ).length;
    const olderCount = feedingActivities.filter(
      (a) =>
        a.startedAt.getTime() <= Date.now() - 3 * 24 * 60 * 60 * 1000 &&
        a.startedAt.getTime() > Date.now() - 6 * 24 * 60 * 60 * 1000,
    ).length;

    const trend =
      recentCount > olderCount * 1.2
        ? 'increasing'
        : recentCount < olderCount * 0.8
          ? 'decreasing'
          : 'stable';

    return {
      averageInterval: Math.round(averageInterval * 10) / 10,
      averageDuration: Math.round(averageDuration),
      totalFeedings: feedingActivities.length,
      feedingMethod,
      consistency: Math.round(consistency * 100) / 100,
      trend,
    };
  }

  /**
   * Analyze diaper patterns
   */
  async analyzeDiaperPatterns(
    activities: Activity[],
    child: Child,
  ): Promise<DiaperPattern | null> {
    const diaperActivities = activities.filter(
      (a) => a.type === ActivityType.DIAPER,
    );

    if (diaperActivities.length < 3) {
      return null;
    }

    const days = Math.max(
      1,
      (Date.now() - diaperActivities[0].startedAt.getTime()) /
        (1000 * 60 * 60 * 24),
    );

    // Count wet and dirty diapers
    const wetCount = diaperActivities.filter(
      (a) => a.metadata?.type === 'wet' || a.metadata?.type === 'both',
    ).length;
    const dirtyCount = diaperActivities.filter(
      (a) => a.metadata?.type === 'dirty' || a.metadata?.type === 'both',
    ).length;

    const wetDiapersPerDay = wetCount / days;
    const dirtyDiapersPerDay = dirtyCount / days;

    // Calculate average interval
    const intervals: number[] = [];
    for (let i = 1; i < diaperActivities.length; i++) {
      const interval =
        (diaperActivities[i].startedAt.getTime() -
          diaperActivities[i - 1].startedAt.getTime()) /
        (1000 * 60 * 60); // hours
      intervals.push(interval);
    }
    const averageInterval =
      intervals.reduce((sum, i) => sum + i, 0) / intervals.length;

    // Determine if pattern is healthy (age-appropriate)
    const ageInMonths = this.calculateAgeInMonths(child.birthDate);
    const isHealthy =
      wetDiapersPerDay >= 4 && // Minimum expected wet diapers
      dirtyDiapersPerDay >= (ageInMonths < 2 ? 2 : 1); // Newborns: 2+, older: 1+

    const notes = isHealthy
      ? 'Diaper output is within healthy range'
      : 'Diaper output may be below expected range - consult pediatrician if concerned';

    return {
      wetDiapersPerDay: Math.round(wetDiapersPerDay * 10) / 10,
      dirtyDiapersPerDay: Math.round(dirtyDiapersPerDay * 10) / 10,
      averageInterval: Math.round(averageInterval * 10) / 10,
      isHealthy,
      notes,
    };
  }

  /**
   * Generate recommendations based on patterns
   */
  private generateRecommendations(
    sleep: SleepPattern | null,
    feeding: FeedingPattern | null,
    diaper: DiaperPattern | null,
    child: Child,
  ): string[] {
    const recommendations: string[] = [];

    // Sleep recommendations
    if (sleep) {
      if (sleep.consistency < 0.7) {
        recommendations.push(
          'Try to maintain a consistent bedtime routine to improve sleep quality',
        );
      }
      if (sleep.nightWakings > 3) {
        recommendations.push(
          'Consider techniques to reduce night wakings, such as dream feeding',
        );
      }
      if (sleep.averageDuration < 600) {
        // Less than 10 hours
        recommendations.push(
          'Your child may benefit from earlier bedtimes or longer naps',
        );
      }
    }

    // Feeding recommendations
    if (feeding) {
      if (feeding.consistency < 0.6) {
        recommendations.push(
          'Try feeding on a more regular schedule to establish a routine',
        );
      }
      if (feeding.trend === 'decreasing') {
        recommendations.push(
          'Monitor feeding frequency - consult pediatrician if decline continues',
        );
      }
    }

    // Diaper recommendations
    if (diaper && !diaper.isHealthy) {
      recommendations.push(
        'Diaper output appears low - ensure adequate hydration and consult pediatrician',
      );
    }

    return recommendations;
  }

  /**
   * Detect concerns based on patterns
   */
  private detectConcerns(
    sleep: SleepPattern | null,
    feeding: FeedingPattern | null,
    diaper: DiaperPattern | null,
    child: Child,
  ): string[] {
    const concerns: string[] = [];

    // Sleep concerns
    if (sleep) {
      if (sleep.trend === 'declining') {
        concerns.push('Sleep duration has been decreasing');
      }
      if (sleep.nightWakings > 5) {
        concerns.push('Frequent night wakings detected');
      }
    }

    // Feeding concerns
    if (feeding) {
      if (feeding.trend === 'decreasing' && feeding.totalFeedings < 15) {
        concerns.push('Feeding frequency appears to be decreasing');
      }
    }

    // Diaper concerns
    if (diaper && !diaper.isHealthy) {
      concerns.push('Diaper output is below expected range');
    }

    return concerns;
  }

  /**
   * Calculate average time from array of dates
   */
  private calculateAverageTime(dates: Date[]): string {
    if (dates.length === 0) return '00:00';

    // Convert to minutes from midnight
    const minutes = dates.map((d) => d.getHours() * 60 + d.getMinutes());
    const avgMinutes = minutes.reduce((sum, m) => sum + m, 0) / minutes.length;

    const hours = Math.floor(avgMinutes / 60);
    const mins = Math.round(avgMinutes % 60);

    return `${hours.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}`;
  }

  /**
   * Calculate standard deviation
   */
  private calculateStdDev(values: number[]): number {
    const avg = values.reduce((sum, v) => sum + v, 0) / values.length;
    const squareDiffs = values.map((v) => Math.pow(v - avg, 2));
    const avgSquareDiff =
      squareDiffs.reduce((sum, d) => sum + d, 0) / values.length;
    return Math.sqrt(avgSquareDiff);
  }

  /**
   * Calculate age in months
   */
  private calculateAgeInMonths(birthDate: Date): number {
    const now = new Date();
    const months =
      (now.getFullYear() - birthDate.getFullYear()) * 12 +
      (now.getMonth() - birthDate.getMonth());
    return months;
  }

  /**
   * Detect growth spurt based on feeding and sleep patterns
   *
   * Growth spurt indicators:
   * - Increased feeding frequency (20%+ more than baseline)
   * - Increased feeding duration
   * - Disrupted sleep patterns (more night wakings, shorter naps)
   * - Increased fussiness (detected through note patterns if implemented)
   * - Typically occurs at: 2-3 weeks, 6 weeks, 3 months, 6 months, 9 months
   */
  private async detectGrowthSpurt(
    activities: Activity[],
    sleepPattern: SleepPattern | null,
    feedingPattern: FeedingPattern | null,
    child: Child,
  ): Promise<GrowthSpurtDetection> {
    const indicators: string[] = [];
    let confidenceScore = 0;
    const ageInMonths = this.calculateAgeInMonths(child.birthDate);
    const ageInWeeks = Math.floor(ageInMonths * 4.33);

    // Check if child is at a typical growth spurt age
    const typicalGrowthSpurtWeeks = [2, 3, 6, 12, 16, 24, 36];
    const isTypicalAge = typicalGrowthSpurtWeeks.some(
      (week) => Math.abs(ageInWeeks - week) <= 1,
    );

    if (isTypicalAge) {
      indicators.push(`Child is ${ageInWeeks} weeks old (typical growth spurt age)`);
      confidenceScore += 0.2;
    }

    // Analyze feeding changes (compare last 3 days vs previous 4 days)
    if (feedingPattern && activities.length >= 14) {
      const now = new Date();
      const last3Days = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000);
      const last7Days = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);

      const recentFeedings = activities.filter(
        (a) => a.type === ActivityType.FEEDING && a.startedAt >= last3Days,
      );
      const previousFeedings = activities.filter(
        (a) =>
          a.type === ActivityType.FEEDING &&
          a.startedAt >= last7Days &&
          a.startedAt < last3Days,
      );

      const recentFeedingsPerDay = recentFeedings.length / 3;
      const previousFeedingsPerDay = previousFeedings.length / 4;

      // Check for 20%+ increase in feeding frequency
      if (
        previousFeedingsPerDay > 0 &&
        recentFeedingsPerDay / previousFeedingsPerDay >= 1.2
      ) {
        const increasePercent = Math.round(
          ((recentFeedingsPerDay - previousFeedingsPerDay) /
            previousFeedingsPerDay) *
            100,
        );
        indicators.push(
          `Feeding frequency increased by ${increasePercent}% (${recentFeedingsPerDay.toFixed(1)}/day vs ${previousFeedingsPerDay.toFixed(1)}/day)`,
        );
        confidenceScore += 0.3;
      }

      // Check for increased feeding duration
      const recentAvgDuration =
        recentFeedings
          .filter((f) => f.endedAt)
          .reduce((sum, f) => {
            const duration =
              (f.endedAt!.getTime() - f.startedAt.getTime()) / 1000 / 60;
            return sum + duration;
          }, 0) / recentFeedings.filter((f) => f.endedAt).length;

      const previousAvgDuration =
        previousFeedings
          .filter((f) => f.endedAt)
          .reduce((sum, f) => {
            const duration =
              (f.endedAt!.getTime() - f.startedAt.getTime()) / 1000 / 60;
            return sum + duration;
          }, 0) / previousFeedings.filter((f) => f.endedAt).length;

      if (
        !isNaN(recentAvgDuration) &&
        !isNaN(previousAvgDuration) &&
        recentAvgDuration > previousAvgDuration * 1.15
      ) {
        indicators.push(
          `Feeding duration increased (${recentAvgDuration.toFixed(1)} min vs ${previousAvgDuration.toFixed(1)} min)`,
        );
        confidenceScore += 0.2;
      }
    }

    // Analyze sleep disruptions
    if (sleepPattern) {
      // Check for declining sleep trend
      if (sleepPattern.trend === 'declining') {
        indicators.push('Sleep pattern showing decline');
        confidenceScore += 0.15;
      }

      // Check for increased night wakings
      if (sleepPattern.nightWakings > 3) {
        indicators.push(
          `Increased night wakings (${sleepPattern.nightWakings} times)`,
        );
        confidenceScore += 0.15;
      }

      // Check for low consistency (indicating disrupted sleep)
      if (sleepPattern.consistency < 0.6) {
        indicators.push(
          `Low sleep consistency (${Math.round(sleepPattern.consistency * 100)}%)`,
        );
        confidenceScore += 0.1;
      }
    }

    // Determine if it's likely a growth spurt
    const isLikelyGrowthSpurt = confidenceScore >= 0.4 && indicators.length >= 2;

    // Generate recommendations
    const recommendations: string[] = [];
    if (isLikelyGrowthSpurt) {
      recommendations.push(
        'Feed on demand - growth spurts require increased nutrition',
      );
      recommendations.push(
        'Expect temporary sleep disruptions - this is normal during growth spurts',
      );
      recommendations.push(
        'Stay hydrated if breastfeeding - increased feeding demands more milk production',
      );
      recommendations.push(
        'Growth spurts typically last 2-3 days, sometimes up to a week',
      );
      recommendations.push(
        'Contact your pediatrician if symptoms persist beyond 1 week',
      );
    }

    // Determine expected duration based on age
    let expectedDuration = '2-3 days';
    if (ageInWeeks < 4) {
      expectedDuration = '1-2 days';
    } else if (ageInWeeks >= 12) {
      expectedDuration = '3-7 days';
    }

    return {
      isLikelyGrowthSpurt,
      confidence: Math.min(confidenceScore, 1.0),
      indicators,
      expectedDuration,
      recommendations,
    };
  }
}
