import Foundation

public struct LogEntry: Identifiable, Equatable, Sendable {
    public let id = UUID()
    public let timestamp: Date
    public let message: String
    public let level: LogLevel

    public enum LogLevel: Sendable {
        case info
        case success
        case warning
        case error

        public var icon: String {
            switch self {
            case .info: return "info.circle"
            case .success: return "checkmark.circle"
            case .warning: return "exclamationmark.triangle"
            case .error: return "xmark.circle"
            }
        }
    }

    public static func == (lhs: LogEntry, rhs: LogEntry) -> Bool {
        lhs.id == rhs.id
    }
}

@MainActor
public class ActivityLog: ObservableObject {
    public static let shared = ActivityLog()

    @Published public private(set) var entries: [LogEntry] = []

    private let maxEntries = 50

    private init() {}

    public func log(_ message: String, level: LogEntry.LogLevel = .info) {
        let entry = LogEntry(timestamp: Date(), message: message, level: level)
        entries.insert(entry, at: 0)
        if entries.count > maxEntries {
            entries = Array(entries.prefix(maxEntries))
        }
        NSLog("ActivityLog: [\(level)] \(message)")
    }

    public func info(_ message: String) { log(message, level: .info) }
    public func success(_ message: String) { log(message, level: .success) }
    public func warning(_ message: String) { log(message, level: .warning) }
    public func error(_ message: String) { log(message, level: .error) }
    public func clear() { entries.removeAll() }
}
