import AppKit
import Foundation
import UserNotifications

final class NotificationManager {
    static let shared = NotificationManager()

    private let notificationCenter = UNUserNotificationCenter.current()

    private init() {}

    func requestPermission() async {
        _ = try? await notificationCenter.requestAuthorization(options: [.alert, .sound])
    }

    func notify(from senderName: String, body: String) {
        guard !NSApp.isActive else {
            return
        }

        let content = UNMutableNotificationContent()
        content.title = senderName
        content.body = String(body.prefix(100))
        content.sound = .default

        let request = UNNotificationRequest(
            identifier: UUID().uuidString,
            content: content,
            trigger: nil
        )
        notificationCenter.add(request)
    }
}
