import Foundation

// Used to create and send hook session context to analytics
public struct HookSessionContext: Codable {
    public let hookSessionId: String
    public let previousHookSessionId: String?
    public let startTime: TimeInterval
    public let endTime: TimeInterval?
    public let playDuration: TimeInterval? // For the segment interrupted by the action
    public let totalPlayDuration: TimeInterval? // Accumulated for the hook session
    public let isUserHookOwner: Bool
    public let recommendationItemId: String?
    public let volume: Float? // System audio volume at time of event
    public let isMuted: Bool // Before tapping to unmute
    public let contextType: String?
    public let contextId: String?
    public let sourceUrl: String? // For deeplink tracking
    public let navigationIntent: String? // "normal", "open_comments", "reply_to_comment"
    public let targetCommentId: String? // For comment-specific navigation
    public let clipId: String? // When navigating to the full song
    public let previousClipId: String? // When navigating from the full song
    public let previousSongSessionId: String? // When navigating from the full song
    public let timeToFirstFrameSecs: TimeInterval? // Time from play intent to actual playback start

    public init(
        hookSessionId: String,
        previousHookSessionId: String? = nil,
        startTime: TimeInterval,
        endTime: TimeInterval? = nil,
        playDuration: TimeInterval? = nil,
        totalPlayDuration: TimeInterval? = nil,
        isUserHookOwner: Bool,
        recommendationItemId: String? = nil,
        volume: Float? = nil,
        isMuted: Bool,
        contextType: String? = nil,
        contextId: String? = nil,
        sourceUrl: String? = nil,
        navigationIntent: String? = nil,
        targetCommentId: String? = nil,
        clipId: String? = nil,
        previousClipId: String? = nil,
        previousSongSessionId: String? = nil,
        timeToFirstFrameSecs: TimeInterval? = nil
    ) {
        self.hookSessionId = hookSessionId
        self.previousHookSessionId = previousHookSessionId
        self.startTime = startTime
        self.endTime = endTime
        self.playDuration = playDuration
        self.totalPlayDuration = totalPlayDuration
        self.isUserHookOwner = isUserHookOwner
        self.recommendationItemId = recommendationItemId
        self.volume = volume
        self.isMuted = isMuted
        self.contextType = contextType
        self.contextId = contextId
        self.sourceUrl = sourceUrl
        self.navigationIntent = navigationIntent
        self.targetCommentId = targetCommentId
        self.clipId = clipId
        self.previousClipId = previousClipId
        self.previousSongSessionId = previousSongSessionId
        self.timeToFirstFrameSecs = timeToFirstFrameSecs
    }

    public func toJsonString() -> String {
        let encoder = JSONEncoder()
        if let data = try? encoder.encode(self),
           let string = String(data: data, encoding: .utf8)
        {
            return string
        }
        return "{}"
    }
}
