import Foundation
import Utilities

/// Represents the context for any user session or interaction (songs, hooks, etc.)
public struct SessionContext: Equatable, Sendable {
    public let source: ContextSource
    public let navigationIntent: NavigationIntent?
    public let sourceUrl: String?

    public init(
        source: ContextSource,
        navigationIntent: NavigationIntent? = nil,
        sourceUrl: String? = nil
    ) {
        self.source = source
        self.navigationIntent = navigationIntent
        self.sourceUrl = sourceUrl
    }
}

public enum NavigationIntent: Codable, Equatable, Sendable {
    case openComments(replyToCommentId: String?)

    public var analyticsIntent: String {
        switch self {
        case .openComments(let targetCommentId):
            if targetCommentId != nil {
                return "reply_to_comment"
            } else {
                return "open_comments"
            }
        }
    }

    // Helper to determine comment ID that we're replying to
    // for analytics
    public var analyticsTargetCommentId: String? {
        switch self {
        case .openComments(let targetCommentId):
            return targetCommentId
        }
    }
}

public extension SessionContext {
    // Use contextType and contextId from `contextSource` but
    // everything else comes from the SessionContext itself.
    var analyticsContext: AnalyticsContext {
        return AnalyticsContext(
            source: source,
            sourceUrl: sourceUrl,
            navigationIntent: navigationIntent?.analyticsIntent,
            targetCommentId: navigationIntent?.analyticsTargetCommentId
        )
    }
}
