import Foundation

/// Simpele append-only bestand-logger voor diagnose.
/// Schrijft naar ~/Library/Application Support/com.prakashjoshipax.VoiceInk/proat-debug.log
/// zodat we transcriptie-pad + biasing kunnen verifieren zonder Apple's unified log.
enum ProatDebugLog {
    private static let queue = DispatchQueue(label: "com.noaber.proat.debuglog")

    static var fileURL: URL {
        let dir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
            .appendingPathComponent("com.prakashjoshipax.VoiceInk", isDirectory: true)
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return dir.appendingPathComponent("proat-debug.log")
    }

    static func write(_ message: String) {
        queue.async {
            let ts = ISO8601DateFormatter().string(from: Date())
            let line = "[\(ts)] \(message)\n"
            guard let data = line.data(using: .utf8) else { return }
            let url = fileURL
            if let handle = try? FileHandle(forWritingTo: url) {
                defer { try? handle.close() }
                _ = try? handle.seekToEnd()
                handle.write(data)
            } else {
                try? data.write(to: url)
            }
        }
    }
}
