bear-cli 0.3.1

A native Rust CLI for Bear.app on macOS — SQLite for reads, CloudKit REST API for writes
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result, anyhow, bail};
use reqwest::blocking::Client;
use uuid::Uuid;

use super::auth::AuthConfig;
use super::models::*;
use super::vector_clock;

pub const API_TOKEN: &str = "ce59f955ec47e744f720aa1d2816a4e985e472d8b859b6c7a47b81fd36646307";
const BASE_URL: &str =
    "https://api.apple-cloudkit.com/database/1/iCloud.net.shinyfrog.bear/production/private";
const DEVICE_NAME: &str = "Bear CLI";

pub struct CloudKitClient {
    http: Client,
    auth: AuthConfig,
}

impl CloudKitClient {
    pub fn new(auth: AuthConfig) -> Result<Self> {
        let http = Client::builder()
            .user_agent("bear-cli/0.3.0")
            .build()
            .context("failed to build HTTP client")?;
        Ok(Self { http, auth })
    }

    fn url(&self, path: &str) -> String {
        // Apple CloudKit decodes '+' as space — must percent-encode it explicitly.
        let token = self.auth.ck_web_auth_token.replace('+', "%2B");
        let api = API_TOKEN.replace('+', "%2B");
        format!("{BASE_URL}{path}?ckWebAuthToken={token}&ckAPIToken={api}")
    }

    fn post<Req, Res>(&self, path: &str, body: &Req) -> Result<Res>
    where
        Req: serde::Serialize,
        Res: serde::de::DeserializeOwned,
    {
        let resp = self
            .http
            .post(self.url(path))
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .with_context(|| format!("HTTP POST {path} failed"))?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            bail!("CloudKit {path} returned {status}: {body}");
        }

        resp.json::<Res>()
            .with_context(|| format!("failed to parse response from {path}"))
    }

    pub fn modify(&self, ops: Vec<ModifyOperation>) -> Result<Vec<CkRecord>> {
        let req = ModifyRequest {
            operations: ops,
            zone_id: ZoneId::default(),
        };
        let resp: ModifyResponse = self.post("/records/modify", &req)?;

        // Surface per-record errors
        for rec in &resp.records {
            if let Some(code) = &rec.server_error_code {
                bail!(
                    "CloudKit record error on {}: {} — {}",
                    rec.record_name,
                    code,
                    rec.reason.as_deref().unwrap_or("")
                );
            }
        }
        Ok(resp.records)
    }

    pub fn query(&self, req: QueryRequest) -> Result<QueryResponse> {
        self.post("/records/query", &req)
    }

    pub fn list_notes(
        &self,
        include_trashed: bool,
        include_archived: bool,
        limit: Option<usize>,
    ) -> Result<Vec<CkRecord>> {
        let mut filters = Vec::new();
        if !include_trashed {
            filters.push(CkFilter {
                field_name: "trashed".into(),
                comparator: "EQUALS".into(),
                field_value: CkFilterValue {
                    value: 0.into(),
                    kind: "INT64".into(),
                },
            });
        }
        if !include_archived {
            filters.push(CkFilter {
                field_name: "archived".into(),
                comparator: "EQUALS".into(),
                field_value: CkFilterValue {
                    value: 0.into(),
                    kind: "INT64".into(),
                },
            });
        }

        let mut records = Vec::new();
        let mut continuation_marker = None;

        loop {
            let remaining = limit.map(|n| n.saturating_sub(records.len()));
            if matches!(remaining, Some(0)) {
                break;
            }

            let req = QueryRequest {
                zone_id: ZoneId::default(),
                query: CkQuery {
                    record_type: "SFNote".into(),
                    filter_by: filters.clone(),
                    sort_by: vec![CkSort {
                        field_name: "sf_modificationDate".into(),
                        ascending: false,
                    }],
                },
                results_limit: Some(remaining.unwrap_or(200).min(200)),
                desired_keys: Some(vec![
                    "uniqueIdentifier".into(),
                    "title".into(),
                    "textADP".into(),
                    "subtitleADP".into(),
                    "sf_creationDate".into(),
                    "sf_modificationDate".into(),
                    "trashed".into(),
                    "archived".into(),
                    "pinned".into(),
                    "locked".into(),
                    "encrypted".into(),
                    "todoCompleted".into(),
                    "todoIncompleted".into(),
                    "tagsStrings".into(),
                    "conflictUniqueIdentifier".into(),
                ]),
                continuation_marker,
            };

            let resp = self.query(req)?;
            records.extend(resp.records);
            continuation_marker = resp.continuation_marker;

            if continuation_marker.is_none() {
                break;
            }
        }

        Ok(records)
    }

    pub fn list_tags(&self) -> Result<Vec<CkRecord>> {
        let mut records = Vec::new();
        let mut marker = None;

        loop {
            let resp = self.query(QueryRequest {
                zone_id: ZoneId::default(),
                query: CkQuery {
                    record_type: "SFNoteTag".into(),
                    filter_by: vec![],
                    sort_by: vec![CkSort {
                        field_name: "name".into(),
                        ascending: true,
                    }],
                },
                results_limit: Some(500),
                desired_keys: Some(vec!["name".into(), "sf_modificationDate".into()]),
                continuation_marker: marker,
            })?;
            records.extend(resp.records);
            marker = resp.continuation_marker;
            if marker.is_none() {
                break;
            }
        }

        Ok(records)
    }

    pub fn lookup(&self, record_names: &[&str]) -> Result<Vec<CkRecord>> {
        let req = LookupRequest {
            records: record_names
                .iter()
                .map(|n| LookupRecord {
                    record_name: n.to_string(),
                })
                .collect(),
            zone_id: ZoneId::default(),
        };
        let resp: LookupResponse = self.post("/records/lookup", &req)?;
        Ok(resp.records)
    }

    pub fn fetch_note(&self, record_name: &str) -> Result<CkRecord> {
        let records = self.lookup(&[record_name])?;
        records
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("note not found: {record_name}"))
    }

    /// Upload a file to CloudKit asset storage. Returns the receipt to embed in a record field.
    pub fn upload_asset(
        &self,
        record_name: &str,
        record_type: &str,
        data: &[u8],
        mime_type: &str,
    ) -> Result<AssetReceipt> {
        // Phase 1: request a signed upload URL
        let req = AssetUploadRequest {
            zone_id: ZoneId::default(),
            tokens: vec![AssetToken {
                record_type: record_type.to_string(),
                record_name: record_name.to_string(),
                field_name: "file".to_string(),
            }],
        };
        let resp: AssetUploadResponse = self.post("/assets/upload", &req)?;
        let token = resp
            .tokens
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("no upload token returned"))?;

        // Phase 2: upload raw bytes to the signed URL
        let upload_resp = self
            .http
            .post(&token.url)
            .header("Content-Type", mime_type)
            .body(data.to_vec())
            .send()
            .context("asset upload POST failed")?;

        let status = upload_resp.status();
        if !status.is_success() {
            let body = upload_resp.text().unwrap_or_default();
            bail!("asset upload returned {status}: {body}");
        }

        let result: AssetUploadResult = upload_resp
            .json()
            .context("failed to parse upload receipt")?;
        Ok(result.single_file)
    }

    /// Create a brand-new note. Returns the created record.
    pub fn create_note(
        &self,
        text: &str,
        tag_uuids: Vec<String>,
        tag_names: Vec<String>,
    ) -> Result<CkRecord> {
        let now_ms = now_ms();
        let note_uuid = Uuid::new_v4().to_string().to_uppercase();
        let title = extract_title(text);
        let subtitle = extract_subtitle(text);
        let clock = vector_clock::increment(None, &vector_clock::local_device_id())?;

        let mut fields: Fields = HashMap::new();
        fields.insert("uniqueIdentifier".into(), CkField::string(&note_uuid));
        fields.insert("title".into(), CkField::string(&title));
        fields.insert("subtitleADP".into(), CkField::string_encrypted(&subtitle));
        fields.insert("textADP".into(), CkField::string_encrypted(text));
        fields.insert("tags".into(), CkField::string_list(tag_uuids));
        fields.insert("tagsStrings".into(), CkField::string_list(tag_names));
        fields.insert("files".into(), CkField::string_list(vec![]));
        fields.insert("linkedBy".into(), CkField::string_list(vec![]));
        fields.insert("linkingTo".into(), CkField::string_list(vec![]));
        fields.insert("pinnedInTagsStrings".into(), CkField::string_list(vec![]));
        fields.insert("vectorClock".into(), CkField::bytes(&clock));
        fields.insert("lastEditingDevice".into(), CkField::string(DEVICE_NAME));
        fields.insert("version".into(), CkField::int64(3));
        fields.insert("encrypted".into(), CkField::int64(0));
        fields.insert("locked".into(), CkField::int64(0));
        fields.insert("trashed".into(), CkField::int64(0));
        fields.insert("archived".into(), CkField::int64(0));
        fields.insert("pinned".into(), CkField::int64(0));
        fields.insert("hasImages".into(), CkField::int64(0));
        fields.insert("hasFiles".into(), CkField::int64(0));
        fields.insert("hasSourceCode".into(), CkField::int64(0));
        fields.insert("todoCompleted".into(), CkField::int64(0));
        fields.insert("todoIncompleted".into(), CkField::int64(0));
        fields.insert("sf_creationDate".into(), CkField::timestamp(now_ms));
        fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms + 1));

        let op = ModifyOperation {
            operation_type: "create".into(),
            record: CkRecord {
                record_name: note_uuid.clone(),
                record_type: "SFNote".into(),
                fields,
                record_change_tag: None,
                deleted: false,
                server_error_code: None,
                reason: None,
            },
        };
        let records = self.modify(vec![op])?;
        records
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("no record returned from create"))
    }

    /// Update a note's text. Fetches the current record first to obtain the recordChangeTag
    /// and existing vector clock, then writes back the updated content.
    pub fn update_note_text(&self, record_name: &str, new_text: &str) -> Result<CkRecord> {
        let current = self.fetch_note(record_name)?;
        let change_tag = current
            .record_change_tag
            .clone()
            .ok_or_else(|| anyhow!("note {record_name} has no recordChangeTag"))?;
        let existing_clock = current.str_field("vectorClock");
        let clock = vector_clock::increment(existing_clock, &vector_clock::local_device_id())?;

        let title = extract_title(new_text);
        let subtitle = extract_subtitle(new_text);
        let todo_counts = count_todos(new_text);
        let now_ms = now_ms();

        let mut fields: Fields = HashMap::new();
        fields.insert("textADP".into(), CkField::string_encrypted(new_text));
        fields.insert("text".into(), CkField::string_null());
        fields.insert("title".into(), CkField::string(&title));
        fields.insert("subtitleADP".into(), CkField::string_encrypted(&subtitle));
        fields.insert("subtitle".into(), CkField::string_null());
        fields.insert("vectorClock".into(), CkField::bytes(&clock));
        fields.insert("lastEditingDevice".into(), CkField::string(DEVICE_NAME));
        fields.insert("version".into(), CkField::int64(3));
        fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms));
        fields.insert("todoCompleted".into(), CkField::int64(todo_counts.0));
        fields.insert("todoIncompleted".into(), CkField::int64(todo_counts.1));
        fields.insert(
            "uniqueIdentifier".into(),
            CkField::string(current.str_field("uniqueIdentifier").unwrap_or(record_name)),
        );

        let op = ModifyOperation {
            operation_type: "update".into(),
            record: CkRecord {
                record_name: record_name.to_string(),
                record_type: "SFNote".into(),
                fields,
                record_change_tag: Some(change_tag),
                deleted: false,
                server_error_code: None,
                reason: None,
            },
        };
        let records = self.modify(vec![op])?;
        records
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("no record returned from update"))
    }

    /// Attach a file to a note. Uploads the asset, creates the file record, and
    /// updates the note's markdown — all in one atomic `records/modify` call.
    pub fn attach_file(
        &self,
        note_record_name: &str,
        filename: &str,
        data: &[u8],
        position: AttachPosition,
    ) -> Result<()> {
        // Determine record type and mime type from extension
        let ext = std::path::Path::new(filename)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();
        let is_image = matches!(
            ext.as_str(),
            "jpg" | "jpeg" | "png" | "gif" | "webp" | "heic" | "tiff"
        );
        let record_type = if is_image {
            "SFNoteImage"
        } else {
            "SFNoteGenericFile"
        };
        let mime_type = mime_for_ext(&ext);

        // Upload asset (2-phase)
        let file_record_uuid = Uuid::new_v4().to_string().to_uppercase();
        let receipt = self.upload_asset(&file_record_uuid, record_type, data, &mime_type)?;
        let file_size = receipt.size;

        // Fetch current note to get change tag and existing content
        let note = self.fetch_note(note_record_name)?;
        let change_tag = note
            .record_change_tag
            .clone()
            .ok_or_else(|| anyhow!("note has no recordChangeTag"))?;
        let existing_clock = note.str_field("vectorClock");
        let clock = vector_clock::increment(existing_clock, &vector_clock::local_device_id())?;

        // Build updated note text with file embedded
        let current_text = note.str_field("textADP").unwrap_or("").to_string();
        let embed = if is_image {
            format!("![{filename}]({filename})<!-- {{\"preview\":\"true\",\"embed\":\"true\"}} -->")
        } else {
            format!("[{filename}]({filename})<!-- {{\"preview\":\"true\",\"embed\":\"true\"}} -->")
        };
        let new_text = match position {
            AttachPosition::Append => format!("{current_text}\n{embed}"),
            AttachPosition::Prepend => {
                // Insert after the first heading line if present
                let mut lines = current_text.lines();
                let first = lines.next().unwrap_or("").to_string();
                let rest: String = lines.collect::<Vec<_>>().join("\n");
                if first.starts_with('#') {
                    format!("{first}\n{embed}\n{rest}")
                } else {
                    format!("{embed}\n{current_text}")
                }
            }
        };

        // Update files list on the note
        let mut files_list: Vec<String> = note
            .fields
            .get("files")
            .and_then(|f| f.value.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(str::to_string))
                    .collect()
            })
            .unwrap_or_default();
        files_list.push(file_record_uuid.clone());

        let has_images = note.i64_field("hasImages").unwrap_or(0) + if is_image { 1 } else { 0 };
        let has_files = note.i64_field("hasFiles").unwrap_or(0) + if is_image { 0 } else { 1 };
        let now_ms = now_ms();
        let title = extract_title(&new_text);
        let subtitle = extract_subtitle(&new_text);
        let todo_counts = count_todos(&new_text);

        // Build file record fields
        let mut file_fields: Fields = std::collections::HashMap::new();
        file_fields.insert(
            "uniqueIdentifier".into(),
            CkField::string(&file_record_uuid),
        );
        file_fields.insert("filenameADP".into(), CkField::string_encrypted(filename));
        file_fields.insert("normalizedFileExtension".into(), CkField::string(&ext));
        file_fields.insert("fileSize".into(), CkField::int64(file_size));
        file_fields.insert("file".into(), CkField::asset_id(receipt));
        file_fields.insert(
            "noteUniqueIdentifier".into(),
            CkField::string(
                note.str_field("uniqueIdentifier")
                    .unwrap_or(note_record_name),
            ),
        );
        file_fields.insert("index".into(), CkField::int64(0));
        file_fields.insert("unused".into(), CkField::int64(0));
        file_fields.insert("uploaded".into(), CkField::int64(1));
        file_fields.insert("uploadedDate".into(), CkField::timestamp(now_ms));
        file_fields.insert("insertionDate".into(), CkField::timestamp(now_ms));
        file_fields.insert("encrypted".into(), CkField::int64(0));
        file_fields.insert(
            "animated".into(),
            CkField::int64(if ext == "gif" { 1 } else { 0 }),
        );
        file_fields.insert("version".into(), CkField::int64(3));
        file_fields.insert("sf_creationDate".into(), CkField::timestamp(now_ms));
        file_fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms + 1));

        // Build updated note fields
        let mut note_fields: Fields = std::collections::HashMap::new();
        note_fields.insert("textADP".into(), CkField::string_encrypted(&new_text));
        note_fields.insert("text".into(), CkField::string_null());
        note_fields.insert("title".into(), CkField::string(&title));
        note_fields.insert("subtitleADP".into(), CkField::string_encrypted(&subtitle));
        note_fields.insert("subtitle".into(), CkField::string_null());
        note_fields.insert("files".into(), CkField::string_list(files_list));
        note_fields.insert("hasImages".into(), CkField::int64(has_images));
        note_fields.insert("hasFiles".into(), CkField::int64(has_files));
        note_fields.insert("vectorClock".into(), CkField::bytes(&clock));
        note_fields.insert("lastEditingDevice".into(), CkField::string(DEVICE_NAME));
        note_fields.insert("version".into(), CkField::int64(3));
        note_fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms + 2));
        note_fields.insert("todoCompleted".into(), CkField::int64(todo_counts.0));
        note_fields.insert("todoIncompleted".into(), CkField::int64(todo_counts.1));
        note_fields.insert(
            "uniqueIdentifier".into(),
            CkField::string(
                note.str_field("uniqueIdentifier")
                    .unwrap_or(note_record_name),
            ),
        );

        // Single atomic modify call: file record + note update
        self.modify(vec![
            ModifyOperation {
                operation_type: "create".into(),
                record: CkRecord {
                    record_name: file_record_uuid,
                    record_type: record_type.to_string(),
                    fields: file_fields,
                    record_change_tag: None,
                    deleted: false,
                    server_error_code: None,
                    reason: None,
                },
            },
            ModifyOperation {
                operation_type: "update".into(),
                record: CkRecord {
                    record_name: note_record_name.to_string(),
                    record_type: "SFNote".into(),
                    fields: note_fields,
                    record_change_tag: Some(change_tag),
                    deleted: false,
                    server_error_code: None,
                    reason: None,
                },
            },
        ])?;

        Ok(())
    }

    /// Move a note to trash (sets trashed=1, trashedDate=now, increments vector clock).
    pub fn trash_note(&self, record_name: &str) -> Result<()> {
        let current = self.fetch_note(record_name)?;
        let change_tag = current
            .record_change_tag
            .clone()
            .ok_or_else(|| anyhow!("note has no recordChangeTag"))?;
        let clock = vector_clock::increment(
            current.str_field("vectorClock"),
            &vector_clock::local_device_id(),
        )?;
        let now_ms = now_ms();

        let mut fields: Fields = HashMap::new();
        fields.insert("trashed".into(), CkField::int64(1));
        fields.insert("trashedDate".into(), CkField::timestamp(now_ms));
        fields.insert("vectorClock".into(), CkField::bytes(&clock));
        fields.insert("lastEditingDevice".into(), CkField::string(DEVICE_NAME));
        fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms + 1));
        fields.insert(
            "uniqueIdentifier".into(),
            CkField::string(current.str_field("uniqueIdentifier").unwrap_or(record_name)),
        );

        self.modify(vec![ModifyOperation {
            operation_type: "update".into(),
            record: CkRecord {
                record_name: record_name.to_string(),
                record_type: "SFNote".into(),
                fields,
                record_change_tag: Some(change_tag),
                deleted: false,
                server_error_code: None,
                reason: None,
            },
        }])?;
        Ok(())
    }

    /// Archive a note.
    pub fn archive_note(&self, record_name: &str) -> Result<()> {
        let current = self.fetch_note(record_name)?;
        let change_tag = current
            .record_change_tag
            .clone()
            .ok_or_else(|| anyhow!("note has no recordChangeTag"))?;
        let clock = vector_clock::increment(
            current.str_field("vectorClock"),
            &vector_clock::local_device_id(),
        )?;
        let now_ms = now_ms();

        let mut fields: Fields = HashMap::new();
        fields.insert("archived".into(), CkField::int64(1));
        fields.insert("archivedDate".into(), CkField::timestamp(now_ms));
        fields.insert("vectorClock".into(), CkField::bytes(&clock));
        fields.insert("lastEditingDevice".into(), CkField::string(DEVICE_NAME));
        fields.insert("sf_modificationDate".into(), CkField::timestamp(now_ms + 1));
        fields.insert(
            "uniqueIdentifier".into(),
            CkField::string(current.str_field("uniqueIdentifier").unwrap_or(record_name)),
        );

        self.modify(vec![ModifyOperation {
            operation_type: "update".into(),
            record: CkRecord {
                record_name: record_name.to_string(),
                record_type: "SFNote".into(),
                fields,
                record_change_tag: Some(change_tag),
                deleted: false,
                server_error_code: None,
                reason: None,
            },
        }])?;
        Ok(())
    }
}

pub enum AttachPosition {
    Append,
    Prepend,
}

pub fn now_ms() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

/// First `# Heading` or first non-empty line.
pub fn extract_title(text: &str) -> String {
    for line in text.lines() {
        let t = line.trim();
        if let Some(stripped) = t.strip_prefix("# ") {
            return stripped.to_string();
        }
        if !t.is_empty() {
            return t.to_string();
        }
    }
    String::new()
}

/// First body line (skipping the title line).
pub fn extract_subtitle(text: &str) -> String {
    let mut past_title = false;
    for line in text.lines() {
        let t = line.trim();
        if !past_title {
            past_title = !t.is_empty();
            continue;
        }
        if !t.is_empty() {
            return t.to_string();
        }
    }
    String::new()
}

fn count_todos(text: &str) -> (i64, i64) {
    let mut done = 0i64;
    let mut todo = 0i64;
    for line in text.lines() {
        let t = line.trim();
        if t.starts_with("- [x]") || t.starts_with("- [X]") {
            done += 1;
        } else if t.starts_with("- [ ]") {
            todo += 1;
        }
    }
    (done, todo)
}

fn mime_for_ext(ext: &str) -> String {
    match ext {
        "jpg" | "jpeg" => "image/jpeg",
        "png" => "image/png",
        "gif" => "image/gif",
        "webp" => "image/webp",
        "heic" => "image/heic",
        "tiff" | "tif" => "image/tiff",
        "pdf" => "application/pdf",
        "txt" => "text/plain",
        _ => "application/octet-stream",
    }
    .to_string()
}