1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/swift

/*
Outputs JSON with the primary event (for bar label) and all remaining events today (for popup).

Output format:
{
  "primary": { "display": "Standup ends in 5 minutes", "url": "https://..." },
  "events": [
    { "time": "9:00 - 9:30 AM", "title": "Standup", "display": "Standup ends in 5 minutes", "url": "https://...", "current": true, "id": "ABC123" },
    { "time": "10:00 - 11:00 AM", "title": "Design Review", "display": "Design Review in 35 minutes", "url": null, "current": false, "id": "DEF456" }
  ]
}

When there are no meetings: { "primary": null, "events": [] }
*/

import Foundation
import EventKit

struct MeetingBarError: Error {
    let message: String
}

// Returns priority for participation status (lower = higher priority)
func participationPriority(for event: EKEvent) -> Int {
    guard let attendees = event.attendees else { return 100 }
    guard let me = attendees.first(where: { $0.isCurrentUser }) else { return 100 }

    switch me.participantStatus {
    case .accepted: return 0
    case .tentative: return 1
    case .pending: return 2
    case .unknown: return 3
    default: return 100
    }
}

class MeetingBar {
    let calendarTitle: String
    let eventStore: EKEventStore

    init(calendarTitle: String, eventStore: EKEventStore) {
        self.calendarTitle = calendarTitle
        self.eventStore = eventStore
    }

    convenience init(calendarTitle: String) throws {
        let eventStore = EKEventStore()
        let status = EKEventStore.authorizationStatus(for: .event)

        switch status {
        case .authorized, .fullAccess, .writeOnly:
            self.init(calendarTitle: calendarTitle, eventStore: eventStore)
        case .denied:
            throw MeetingBarError(
                message: "Calendar access previously denied for this process. "
                    + "Grant it in System Settings > Privacy & Security > Calendars.")
        case .restricted:
            throw MeetingBarError(message: "Calendar access restricted by policy")
        case .notDetermined:
            let semaphore = DispatchSemaphore(value: 0)
            var accessGranted = false

            var requestError: Error?

            eventStore.requestFullAccessToEvents { granted, error in
                accessGranted = granted
                requestError = error
                semaphore.signal()
            }

            semaphore.wait()

            if accessGranted {
                self.init(calendarTitle: calendarTitle, eventStore: eventStore)
            } else {
                let detail = requestError.map { ": \($0.localizedDescription)" } ?? ""
                throw MeetingBarError(
                    message: "Calendar access request was not granted\(detail)")
            }
        @unknown default:
            throw MeetingBarError(message: "Unknown authorization status")
        }
    }

    /// All non-all-day, non-declined events from now until end of day, sorted by start time.
    func remainingEventsToday() throws -> [EKEvent] {
        let currentDate = Date()
        let calendar = Calendar.current

        let calendars = eventStore.calendars(for: .event).filter { $0.title.contains(self.calendarTitle) }
        guard !calendars.isEmpty else {
            throw MeetingBarError(message: "No calendar found for '\(self.calendarTitle)'")
        }

        guard let tomorrow = calendar.date(byAdding: .day, value: 1, to: currentDate),
              let endOfDay = calendar.date(bySettingHour: 0, minute: 0, second: 0, of: tomorrow) else {
            throw MeetingBarError(message: "Failed to calculate date")
        }

        let predicate = eventStore.predicateForEvents(withStart: currentDate, end: endOfDay, calendars: calendars)
        return eventStore.events(matching: predicate)
            .filter { event in
                guard !event.isAllDay else { return false }
                if let attendees = event.attendees,
                   let me = attendees.first(where: { $0.isCurrentUser }),
                   me.participantStatus == .declined {
                    return false
                }
                return true
            }
            .sorted { $0.startDate < $1.startDate }
    }

    /// Smart selection of which event to feature in the bar, using the full priority logic.
    func primaryEvent(from events: [EKEvent]) -> EKEvent? {
        let currentDate = Date()

        // Find current meetings, preferring accepted ones
        let currentMeetings = events.filter { currentDate >= $0.startDate && currentDate <= $0.endDate }
        let currentMeeting = currentMeetings.min { first, second in
            let firstPriority = participationPriority(for: first)
            let secondPriority = participationPriority(for: second)
            if firstPriority != secondPriority {
                return firstPriority < secondPriority
            }
            let firstDuration = first.endDate.timeIntervalSince(first.startDate)
            let secondDuration = second.endDate.timeIntervalSince(second.startDate)
            return firstDuration < secondDuration
        }

        // Find all events starting after current time
        let upcomingEvents = events.filter { $0.startDate > currentDate }
        let nextMeeting = upcomingEvents.min { first, second in
            if first.startDate != second.startDate {
                return first.startDate < second.startDate
            }
            let firstPriority = participationPriority(for: first)
            let secondPriority = participationPriority(for: second)
            if firstPriority != secondPriority {
                return firstPriority < secondPriority
            }
            let firstDuration = first.endDate.timeIntervalSince(first.startDate)
            let secondDuration = second.endDate.timeIntervalSince(second.startDate)
            return firstDuration < secondDuration
        }

        guard let current = currentMeeting else { return nextMeeting }
        guard let next = nextMeeting else { return current }

        let duration = current.endDate.timeIntervalSince(current.startDate)
        let timeUntilNext = next.startDate.timeIntervalSince(currentDate)
        let threshold = 5 * 60 + (duration / 3600) * 10 * 60

        if timeUntilNext < threshold {
            return next
        }

        return current
    }
}

// MARK: - Helpers

func extractMeetingURL(from event: EKEvent) -> String? {
    if let notes = event.notes, let url = extractMeetingURL(from: notes) {
        return url
    }
    if let location = event.location, let url = extractMeetingURL(from: location) {
        return url
    }
    if let url = event.url?.absoluteString {
        if url.contains("zoom.us") || url.contains("vimeo.com") || url.contains("meet.google.com") {
            return url
        }
    }
    return nil
}

func extractMeetingURL(from text: String) -> String? {
    let patterns = [
        "https://[a-zA-Z0-9.-]*\\.?zoom\\.us/[^\\s]+",
        "https://(?:www\\.)?vimeo\\.com/[^\\s]+",
        "https://meet\\.google\\.com/[^\\s]+"
    ]

    for pattern in patterns {
        guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { continue }
        let nsString = text as NSString
        let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsString.length))
        if let match = matches.first {
            let url = nsString.substring(with: match.range)
            return url.trimmingCharacters(in: CharacterSet(charactersIn: "\"'"))
        }
    }
    return nil
}

func formatTimeUntil(_ date: Date, from currentDate: Date) -> String {
    let minutes = Int(date.timeIntervalSince(currentDate) / 60)
    if minutes <= 0 {
        return "now"
    } else if minutes < 60 {
        return minutes == 1 ? "in 1 minute" : "in \(minutes) minutes"
    } else {
        let hours = minutes / 60
        let remainingMinutes = minutes % 60
        return remainingMinutes == 0 ? "in \(hours)h" : "in \(hours)h\(remainingMinutes)m"
    }
}

func jsonString(_ s: String) -> String {
    // Escape backslashes, quotes, and control characters for JSON
    var result = s
    result = result.replacingOccurrences(of: "\\", with: "\\\\")
    result = result.replacingOccurrences(of: "\"", with: "\\\"")
    result = result.replacingOccurrences(of: "\n", with: "\\n")
    result = result.replacingOccurrences(of: "\r", with: "\\r")
    result = result.replacingOccurrences(of: "\t", with: "\\t")
    return "\"\(result)\""
}

func jsonStringOrNull(_ s: String?) -> String {
    guard let s = s else { return "null" }
    return jsonString(s)
}

// MARK: - Main

guard CommandLine.arguments.count > 1 else {
    print("Error: Calendar title argument is required")
    print("Usage: \(CommandLine.arguments[0]) <calendar-title>")
    exit(1)
}

let calendarTitle = CommandLine.arguments[1]

do {
    let manager = try MeetingBar(calendarTitle: calendarTitle)
    let events = try manager.remainingEventsToday()
    var primary = manager.primaryEvent(from: events)
    let currentDate = Date()

    // Check for pinned event override.
    let tmpDir = ProcessInfo.processInfo.environment["TMPDIR"] ?? NSTemporaryDirectory()
    let pinPath = tmpDir + "meetingbar_pin"

    if let pinnedID = try? String(contentsOfFile: pinPath, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines),
       !pinnedID.isEmpty {
        if let pinnedEvent = events.first(where: { $0.eventIdentifier == pinnedID }) {
            primary = pinnedEvent
        } else {
            try? FileManager.default.removeItem(atPath: pinPath)
        }
    }

    let timeFormatter = DateFormatter()
    timeFormatter.dateFormat = "h:mm a"

    // Build primary object
    let primaryJSON: String
    if let event = primary {
        let isCurrentMeeting = currentDate >= event.startDate && currentDate <= event.endDate
        let displayText: String
        if isCurrentMeeting {
            let timeStr = formatTimeUntil(event.endDate, from: currentDate)
            let title = (event.title ?? "Untitled Event").trimmingCharacters(in: .whitespaces)
            displayText = "\(title) ends \(timeStr)"
        } else {
            let timeStr = formatTimeUntil(event.startDate, from: currentDate)
            let title = (event.title ?? "Untitled Event").trimmingCharacters(in: .whitespaces)
            displayText = "\(title) \(timeStr)"
        }
        let url = extractMeetingURL(from: event)
        primaryJSON = "{ \"display\": \(jsonString(displayText)), \"url\": \(jsonStringOrNull(url)) }"
    } else {
        primaryJSON = "null"
    }

    // Build events array
    var eventJSONs: [String] = []
    for event in events {
        let start = timeFormatter.string(from: event.startDate)
        let end = timeFormatter.string(from: event.endDate)
        let time = "\(start) - \(end)"
        let title = (event.title ?? "Untitled Event").trimmingCharacters(in: .whitespaces)
        let url = extractMeetingURL(from: event)
        let isCurrent = currentDate >= event.startDate && currentDate <= event.endDate
        let display: String
        if isCurrent {
            display = "\(title) ends \(formatTimeUntil(event.endDate, from: currentDate))"
        } else {
            display = "\(title) \(formatTimeUntil(event.startDate, from: currentDate))"
        }
        eventJSONs.append("{ \"time\": \(jsonString(time)), \"title\": \(jsonString(title)), \"display\": \(jsonString(display)), \"url\": \(jsonStringOrNull(url)), \"current\": \(isCurrent), \"id\": \(jsonString(event.eventIdentifier)) }")
    }

    let eventsJSON = "[\(eventJSONs.joined(separator: ", "))]"
    print("{ \"primary\": \(primaryJSON), \"events\": \(eventsJSON) }")
} catch let error as MeetingBarError {
    fputs("Error: \(error.message)\n", stderr)
    exit(1)
} catch {
    fputs("Error: \(error)\n", stderr)
    exit(1)
}