import Foundation

struct WatchHistoryEntry: Identifiable, Codable, Hashable, Sendable {
    let id: String
    let name: String
    let streamURL: String
    let iconURL: String?
    let watchedAt: Date
    let duration: TimeInterval
    let progress: TimeInterval
    let isMovie: Bool

    var progressPercent: Double {
        guard duration > 0 else { return 0 }
        return progress / duration
    }

    var isCompleted: Bool {
        progressPercent >= 0.95
    }

    var formattedProgress: String {
        let progressMinutes = Int(progress) / 60
        let progressSeconds = Int(progress) % 60
        return String(format: "%d:%02d", progressMinutes, progressSeconds)
    }

    var formattedDuration: String {
        let durationMinutes = Int(duration) / 60
        let durationSeconds = Int(duration) % 60
        return String(format: "%d:%02d", durationMinutes, durationSeconds)
    }

    init(
        id: String,
        name: String,
        streamURL: String,
        iconURL: String?,
        watchedAt: Date = Date(),
        duration: TimeInterval,
        progress: TimeInterval,
        isMovie: Bool
    ) {
        self.id = id
        self.name = name
        self.streamURL = streamURL
        self.iconURL = iconURL
        self.watchedAt = watchedAt
        self.duration = duration
        self.progress = progress
        self.isMovie = isMovie
    }

    init(from vodItem: VODItem, duration: TimeInterval, progress: TimeInterval) {
        self.id = vodItem.id
        self.name = vodItem.name
        self.streamURL = vodItem.streamURL
        self.iconURL = vodItem.iconURL
        self.watchedAt = Date()
        self.duration = duration
        self.progress = progress
        self.isMovie = vodItem.isMovie
    }
}
