//
//  InsightTipsProviding.swift
//  Andromida
//
//  Protocol for generating contextual tips based on insight metrics.
//

import Foundation

/// Context information for generating insight tips.
struct InsightContext {
    let completionRate: Double
    let currentStreak: Int
    let longestStreak: Int
    let daysActive: Int
    let totalHabits: Int
    let completedToday: Int
}

/// Protocol for generating contextual tips for insights.
protocol InsightTipsProviding {
    /// Generates tips based on the insight card and user context.
    /// - Parameters:
    ///   - card: The insight card to generate tips for
    ///   - context: Current user metrics context
    /// - Returns: Array of tip strings
    func tips(for card: InsightCard, context: InsightContext) -> [String]
}

/// Default implementation providing standard tips.
struct DefaultInsightTipsProvider: InsightTipsProviding {
    func tips(for card: InsightCard, context: InsightContext) -> [String] {
        switch card.title {
        case String(localized: "Active"):
            return activeTips(context: context)
        case String(localized: "Streak"):
            return streakTips(context: context)
        case String(localized: "Habits today"):
            return habitsTips(context: context)
        case String(localized: "Completion"):
            return completionTips(context: context)
        case String(localized: "Days Active"):
            return daysActiveTips(context: context)
        default:
            return []
        }
    }
    
    private func activeTips(context: InsightContext) -> [String] {
        var tips: [String] = []
        if context.totalHabits > 10 {
            tips.append(String(localized: "Consider focusing on fewer habits for better consistency."))
        }
        if context.totalHabits < 3 {
            tips.append(String(localized: "Adding 1-2 more habits could help build momentum."))
        }
        return tips
    }
    
    private func streakTips(context: InsightContext) -> [String] {
        var tips: [String] = []
        if context.currentStreak == 0 {
            tips.append(String(localized: "Complete all habits today to start a new streak."))
        } else if context.currentStreak < context.longestStreak {
            let daysToGo = context.longestStreak - context.currentStreak
            tips.append(String(localized: "\(daysToGo) more days to beat your record!"))
        } else if context.currentStreak == context.longestStreak && context.currentStreak > 0 {
            tips.append(String(localized: "You're at your best streak! Keep it going."))
        }
        return tips
    }
    
    private func habitsTips(context: InsightContext) -> [String] {
        var tips: [String] = []
        let remaining = context.totalHabits - context.completedToday
        if remaining > 0 {
            tips.append(String(localized: "\(remaining) habits left to complete today."))
        } else if context.totalHabits > 0 {
            tips.append(String(localized: "All habits complete! Great work today."))
        }
        return tips
    }
    
    private func completionTips(context: InsightContext) -> [String] {
        var tips: [String] = []
        if context.completionRate < 0.5 {
            tips.append(String(localized: "Try starting with just one habit to build momentum."))
        } else if context.completionRate >= 0.8 {
            tips.append(String(localized: "Excellent consistency! You're building strong habits."))
        }
        return tips
    }
    
    private func daysActiveTips(context: InsightContext) -> [String] {
        var tips: [String] = []
        if context.daysActive >= 7 {
            tips.append(String(localized: "A week of activity shows commitment!"))
        }
        if context.daysActive >= 21 {
            tips.append(String(localized: "21 days is when habits start to stick."))
        }
        return tips
    }
}
