rscalendar 0.1.2

Manage your Google Calendar
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
mod auth;
mod client;
mod config;
mod helpers;
mod models;

use anyhow::{Context, Result, bail};
use chrono::NaiveDate;
use clap::{Args, CommandFactory, Parser, Subcommand};
use serde_json::json;

use client::{GoogleCalendarClient, resolve_calendar_id};
use config::Config;
use helpers::{build_event_patch_payload, parse_event_time, prompt_select, prompt_yes_no_quit};
use models::{print_calendar, print_event};

// --- CLI ---

#[derive(Debug, Parser)]
#[command(
    author,
    version,
    about = "Google Calendar CLI for listing and changing events"
)]
struct Cli {
    /// Show "(built-in)" labels on standard Google Calendar fields.
    #[arg(long, global = true, default_value_t = false)]
    show_builtin: bool,

    /// Output as JSON instead of human-readable text.
    #[arg(long, global = true, default_value_t = false)]
    json: bool,

    /// Suppress informational output; only show data and errors.
    #[arg(long, global = true, default_value_t = false)]
    quiet: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Print version and build information.
    Version,
    /// Print the default configuration file.
    Defconfig,
    /// List all calendars accessible to the authenticated user.
    ListCalendars,
    /// List all events for a calendar.
    List(ListArgs),
    /// Manage individual events.
    Event {
        #[command(subcommand)]
        action: EventAction,
    },
    /// Manage calendars.
    Calendar {
        #[command(subcommand)]
        action: CalendarAction,
    },
    /// Check that events have all required properties based on their type.
    Check(CalendarNameArgs),
    /// Manage event properties.
    Properties {
        #[command(subcommand)]
        action: PropertiesAction,
    },
    /// Interactively move events from one calendar to another.
    MoveEvents(MoveEventsArgs),
    /// Authenticate with Google via OAuth2 and cache the token.
    Auth(AuthArgs),
    /// Generate shell completions.
    Complete {
        /// Shell to generate completions for.
        #[arg(value_enum)]
        shell: clap_complete::Shell,
    },
}

#[derive(Debug, Subcommand)]
enum CalendarAction {
    /// Create a new public calendar.
    Create {
        /// Name of the calendar to create.
        name: String,
    },
}

#[derive(Debug, Subcommand)]
enum EventAction {
    /// Create a new event.
    Create(UpsertArgs),
    /// Update fields on an existing event.
    Update(UpdateArgs),
    /// Delete an event.
    Delete(DeleteArgs),
}

#[derive(Debug, Subcommand)]
enum PropertiesAction {
    /// Add properties to events (validates against config).
    Add(PropertiesAddArgs),
    /// Check that all event properties have keys and values defined in config.
    Check(CalendarNameArgs),
    /// Delete a property from events.
    Delete(PropertiesDeleteArgs),
    /// Rename a property key on events.
    Rename(PropertiesRenameArgs),
    /// Interactively edit properties on each event via TUI menus.
    Edit(CalendarNameArgs),
}

// --- Shared args ---

#[derive(Debug, Args)]
struct CalendarNameArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
}

#[derive(Debug, Args)]
struct MoveEventsArgs {
    /// Source calendar name to move events from.
    #[arg(long)]
    source: String,
    /// Target calendar name to move events into.
    #[arg(long)]
    target: String,
    /// Show what would be done without making changes.
    #[arg(long, default_value_t = false)]
    dry_run: bool,
    /// Move all events without prompting.
    #[arg(long, default_value_t = false)]
    all: bool,
}

#[derive(Debug, Args)]
struct PropertiesAddArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Property key to set (must be defined in config). If omitted, prompts for all missing properties.
    #[arg(long)]
    key: Option<String>,
    /// Property value to set (must be allowed for the key in config). Requires --key.
    #[arg(long)]
    value: Option<String>,
    /// Apply to all events without prompting.
    #[arg(long, default_value_t = false)]
    all: bool,
}

#[derive(Debug, Args)]
struct PropertiesDeleteArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Property key to delete.
    #[arg(long)]
    key: String,
    /// Apply to all events without prompting.
    #[arg(long, default_value_t = false)]
    all: bool,
}

#[derive(Debug, Args)]
struct PropertiesRenameArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Current property key name.
    #[arg(long)]
    from: String,
    /// New property key name.
    #[arg(long)]
    to: String,
    /// Apply to all events without prompting.
    #[arg(long, default_value_t = false)]
    all: bool,
}

#[derive(Debug, Args)]
struct ListArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Only show events starting after this date (RFC3339 or YYYY-MM-DD).
    #[arg(long)]
    starts_after: Option<String>,
    /// Only show events starting before this date (RFC3339 or YYYY-MM-DD).
    #[arg(long)]
    starts_before: Option<String>,
    /// Case-insensitive substring search in summary and description.
    #[arg(long)]
    search: Option<String>,
    /// Filter events that have a specific property key=value pair.
    /// If just KEY is given (no =), filter events that have that key with any value.
    #[arg(long)]
    has_property: Option<String>,
}

#[derive(Debug, Args)]
struct UpsertArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Event summary/title.
    #[arg(long)]
    summary: String,
    /// Event start time. Accepts RFC3339 or YYYY-MM-DD.
    #[arg(long)]
    start: String,
    /// Event end time. Accepts RFC3339 or YYYY-MM-DD. For all-day events,
    /// the provided date is treated as inclusive and converted to Google's
    /// exclusive end-date format.
    #[arg(long)]
    end: String,
    /// Optional description/body text.
    #[arg(long)]
    description: Option<String>,
    /// Optional location string.
    #[arg(long)]
    location: Option<String>,
}

#[derive(Debug, Args)]
struct UpdateArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Event ID to update.
    #[arg(long)]
    event_id: String,
    /// Replacement event summary/title.
    #[arg(long)]
    summary: Option<String>,
    /// Replacement start time. Accepts RFC3339 or YYYY-MM-DD.
    #[arg(long)]
    start: Option<String>,
    /// Replacement end time. Accepts RFC3339 or YYYY-MM-DD.
    #[arg(long)]
    end: Option<String>,
    /// Replacement description.
    #[arg(long)]
    description: Option<String>,
    /// Replacement location.
    #[arg(long)]
    location: Option<String>,
}

#[derive(Debug, Args)]
struct DeleteArgs {
    /// Calendar name (default: from config).
    #[arg(long)]
    calendar_name: Option<String>,
    /// Event ID to delete.
    #[arg(long)]
    event_id: String,
}

#[derive(Debug, Args)]
struct AuthArgs {
    /// Print the authorization URL instead of opening the browser.
    #[arg(long, default_value_t = false)]
    no_browser: bool,
    /// Force re-authentication by removing cached token first.
    #[arg(long, default_value_t = false)]
    force: bool,
}

// --- Helper: fetch events for a calendar by name ---

async fn fetch_events(
    client: &GoogleCalendarClient,
    calendar_name: Option<&str>,
    config: &Config,
) -> Result<(String, Vec<models::CalendarEvent>)> {
    let calendars = client.list_calendars().await?;
    let calendar_id = resolve_calendar_id(&calendars, calendar_name, config)?.to_string();
    let events = client.list_all_events(&calendar_id).await?;
    Ok((calendar_id, events))
}

// --- Filter helpers ---

/// Parse a date string (RFC3339 or YYYY-MM-DD) into a chrono DateTime<chrono::FixedOffset>.
/// For YYYY-MM-DD, midnight UTC is used.
fn parse_filter_date(s: &str) -> Result<chrono::DateTime<chrono::FixedOffset>> {
    if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
        return Ok(dt);
    }
    let nd = NaiveDate::parse_from_str(s, "%Y-%m-%d")
        .with_context(|| format!("cannot parse '{s}' as RFC3339 or YYYY-MM-DD"))?;
    let ndt = nd.and_hms_opt(0, 0, 0).unwrap();
    Ok(chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(
        ndt,
        chrono::FixedOffset::east_opt(0).unwrap(),
    ))
}

/// Extract an event's start time as a DateTime for comparison.
fn event_start_to_datetime(edt: &models::EventDateTime) -> Option<chrono::DateTime<chrono::FixedOffset>> {
    if let Some(ref dt_str) = edt.date_time {
        if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(dt_str) {
            return Some(dt);
        }
    }
    if let Some(ref d_str) = edt.date {
        if let Ok(nd) = NaiveDate::parse_from_str(d_str, "%Y-%m-%d") {
            let ndt = nd.and_hms_opt(0, 0, 0).unwrap();
            return Some(chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(
                ndt,
                chrono::FixedOffset::east_opt(0).unwrap(),
            ));
        }
    }
    None
}

// --- Main ---

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = Config::load();

    if let Command::Complete { shell } = &cli.command {
        clap_complete::generate(*shell, &mut Cli::command(), "rscalendar", &mut std::io::stdout());
        return Ok(());
    }

    if let Command::Version = &cli.command {
        println!("rscalendar {} by {}", env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
        println!("GIT_DESCRIBE: {}", env!("GIT_DESCRIBE"));
        println!("GIT_SHA: {}", env!("GIT_SHA"));
        println!("GIT_BRANCH: {}", env!("GIT_BRANCH"));
        println!("GIT_DIRTY: {}", env!("GIT_DIRTY"));
        println!("RUSTC_SEMVER: {}", env!("RUSTC_SEMVER"));
        println!("RUST_EDITION: {}", env!("RUST_EDITION"));
        println!("BUILD_TIMESTAMP: {}", env!("BUILD_TIMESTAMP"));
        return Ok(());
    }

    if let Command::Defconfig = &cli.command {
        print!(
            "\
# Default calendar name
# calendar_name = \"My Calendar\"

# Don't open browser during auth (useful for headless machines)
# no_browser = false

# Allowed properties for events
# [properties]
# type = [\"teaching\", \"working\", \"call\", \"meeting\"]
# company = [\"Amdocs\", \"Intel\", \"Google\"]
# course = [\"Linux Fundamentals\", \"Advanced Python\"]

# Required properties per event type (used by 'check' command)
# [check]
# teaching = [\"client\", \"company\", \"course\"]
# working = [\"client\", \"company\"]
"
        );
        return Ok(());
    }

    if let Command::Auth(args) = &cli.command {
        auth::cmd_auth(args.no_browser, args.force, &config).await?;
        return Ok(());
    }

    let client = GoogleCalendarClient::from_cache().await?;

    match cli.command {
        Command::ListCalendars => {
            let calendars = client.list_calendars().await?;
            if calendars.is_empty() {
                if !cli.quiet { println!("No calendars found."); }
            } else {
                for cal in &calendars {
                    print_calendar(cal, cli.json);
                }
            }
        }
        Command::List(args) => {
            let (_, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;

            // Parse filter dates once
            let starts_after = args.starts_after.as_deref().map(parse_filter_date)
                .transpose().context("invalid --starts-after date")?;
            let starts_before = args.starts_before.as_deref().map(parse_filter_date)
                .transpose().context("invalid --starts-before date")?;
            let search_lower = args.search.as_ref().map(|s| s.to_lowercase());
            let has_property = args.has_property.as_deref().map(|s| {
                if let Some((k, v)) = s.split_once('=') {
                    (k.to_string(), Some(v.to_string()))
                } else {
                    (s.to_string(), None)
                }
            });

            let filtered: Vec<_> = events.iter().filter(|event| {
                // --starts-after filter
                if let Some(ref after) = starts_after {
                    if let Some(start) = &event.start {
                        let event_dt = event_start_to_datetime(start);
                        if let Some(evt) = event_dt {
                            if evt <= *after {
                                return false;
                            }
                        }
                    }
                }
                // --starts-before filter
                if let Some(ref before) = starts_before {
                    if let Some(start) = &event.start {
                        let event_dt = event_start_to_datetime(start);
                        if let Some(evt) = event_dt {
                            if evt >= *before {
                                return false;
                            }
                        }
                    }
                }
                // --search filter
                if let Some(ref pattern) = search_lower {
                    let summary_match = event.summary.as_ref()
                        .is_some_and(|s| s.to_lowercase().contains(pattern));
                    let desc_match = event.description.as_ref()
                        .is_some_and(|d| d.to_lowercase().contains(pattern));
                    if !summary_match && !desc_match {
                        return false;
                    }
                }
                // --has-property filter
                if let Some((ref key, ref val)) = has_property {
                    let shared = event.extended_properties.as_ref()
                        .and_then(|p| p.shared.as_ref());
                    match val {
                        Some(expected) => {
                            if !shared.is_some_and(|s| s.get(key).is_some_and(|v| v == expected)) {
                                return false;
                            }
                        }
                        None => {
                            if !shared.is_some_and(|s| s.contains_key(key)) {
                                return false;
                            }
                        }
                    }
                }
                true
            }).collect();

            if filtered.is_empty() {
                if !cli.quiet { println!("No events found."); }
            } else {
                for event in &filtered {
                    print_event(event, cli.show_builtin, cli.json);
                }
            }
        }
        Command::Event { action } => match action {
            EventAction::Create(args) => {
                let calendars = client.list_calendars().await?;
                let calendar_id = resolve_calendar_id(&calendars, args.calendar_name.as_deref(), &config)?;
                let start = parse_event_time(&args.start, false)?;
                let end = parse_event_time(&args.end, true)?;
                let event = client.create_event(calendar_id, &args.summary, &start, &end, args.description.as_deref(), args.location.as_deref()).await?;
                if !cli.quiet { println!("Created event:"); }
                print_event(&event, cli.show_builtin, cli.json);
            }
            EventAction::Update(args) => {
                let calendars = client.list_calendars().await?;
                let calendar_id = resolve_calendar_id(&calendars, args.calendar_name.as_deref(), &config)?;
                let payload = build_event_patch_payload(
                    args.summary.as_deref(),
                    args.start.as_deref(),
                    args.end.as_deref(),
                    args.description.as_deref(),
                    args.location.as_deref(),
                )?;
                let event = client.update_event(calendar_id, &args.event_id, &payload).await?;
                if !cli.quiet { println!("Updated event:"); }
                print_event(&event, cli.show_builtin, cli.json);
            }
            EventAction::Delete(args) => {
                let calendars = client.list_calendars().await?;
                let calendar_id = resolve_calendar_id(&calendars, args.calendar_name.as_deref(), &config)?;
                client.delete_event(calendar_id, &args.event_id).await?;
                if !cli.quiet { println!("Deleted event '{}'.", args.event_id); }
            }
        },
        Command::Check(args) => {
            let check_rules = config.check.as_ref()
                .context("no [check] section in config.toml")?;
            if check_rules.is_empty() {
                bail!("no rules defined in [check] section of config.toml");
            }

            let (_, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
            if events.is_empty() {
                if !cli.quiet { println!("No events found."); }
                return Ok(());
            }

            let mut issues = 0u32;
            let mut events_with_issues = 0u32;
            for event in &events {
                let summary = event.summary_or_default();
                let start = event.start_str();
                let shared = event.extended_properties.as_ref().and_then(|p| p.shared.as_ref());

                let event_type = match shared.and_then(|s| s.get("type")) {
                    Some(t) => t,
                    None => {
                        println!("{summary} ({start}):");
                        println!("  - missing 'type' property");
                        issues += 1;
                        events_with_issues += 1;
                        continue;
                    }
                };

                let required = match check_rules.get(event_type) {
                    Some(r) => r,
                    None => {
                        println!("{summary} ({start}):");
                        println!("  - unknown type '{event_type}' (not in [check] config)");
                        issues += 1;
                        events_with_issues += 1;
                        continue;
                    }
                };

                let mut event_issues: Vec<String> = Vec::new();
                for key in required {
                    if shared.and_then(|s| s.get(key)).is_none() {
                        event_issues.push(format!("missing required property '{key}' (required for type '{event_type}')"));
                    }
                }

                if !event_issues.is_empty() {
                    println!("{summary} ({start}):");
                    for issue in &event_issues {
                        println!("  - {issue}");
                    }
                    issues += event_issues.len() as u32;
                    events_with_issues += 1;
                }
            }

            if !cli.quiet {
                if issues == 0 {
                    println!("All {} event(s) pass checks.", events.len());
                } else {
                    println!("\n{issues} issue(s) in {events_with_issues} event(s) out of {}.", events.len());
                }
            }
        }
        Command::Properties { action } => match action {
            PropertiesAction::Add(args) => {
                if args.key.is_some() != args.value.is_some() {
                    bail!("--key and --value must be used together");
                }

                let properties = config.properties.as_ref()
                    .context("no [properties] section in config.toml")?;
                if properties.is_empty() {
                    bail!("no properties defined in [properties] section of config.toml");
                }

                if let (Some(key), Some(value)) = (&args.key, &args.value) {
                    let allowed = properties.get(key)
                        .with_context(|| format!("key '{key}' is not defined in [properties] in config.toml"))?;
                    if !allowed.contains(value) {
                        bail!("value '{value}' is not allowed for key '{key}'. Allowed: {}", allowed.join(", "));
                    }
                }

                let (calendar_id, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
                if events.is_empty() {
                    if !cli.quiet { println!("No events found."); }
                    return Ok(());
                }

                let sorted_keys: Vec<&String> = {
                    let mut keys: Vec<_> = properties.keys().collect();
                    keys.sort();
                    keys
                };

                if !cli.quiet { println!("Adding properties to {} event(s)\n", events.len()); }

                let mut updated = 0u32;
                let mut skipped = 0u32;
                let mut skipped_no_id = 0u32;
                for event in &events {
                    let summary = event.summary_or_default();
                    let start = event.start_str();
                    let event_id = match &event.id {
                        Some(id) => id,
                        None => { skipped_no_id += 1; continue; }
                    };

                    let existing = event.shared_properties();

                    if let (Some(key), Some(value)) = (&args.key, &args.value) {
                        if existing.get(key).is_some_and(|v| v == value) {
                            skipped += 1;
                            continue;
                        }

                        if !args.all {
                            let prompt = format!("Set {key}={value} on '{summary}' ({start})?");
                            match prompt_yes_no_quit(&prompt)? {
                                Some(true) => {}
                                Some(false) => { skipped += 1; continue; }
                                None => {
                                    if !cli.quiet { println!("\nQuit. {updated} updated, {skipped} skipped."); }
                                    return Ok(());
                                }
                            }
                        }

                        let mut new_props = existing;
                        new_props.insert(key.clone(), value.clone());
                        client.patch_event_properties(&calendar_id, event_id, &new_props).await?;
                        if !cli.quiet { println!("  set on: {summary} ({start})"); }
                        updated += 1;
                    } else {
                        eprintln!("Event: {summary} ({start})");
                        if !existing.is_empty() {
                            for (k, v) in &existing {
                                eprintln!("  existing {k}: {v}");
                            }
                        }

                        let mut new_props = existing.clone();
                        let mut changed = false;

                        for key in &sorted_keys {
                            let values = &properties[*key];
                            if existing.contains_key(*key) {
                                continue;
                            }
                            let prompt = format!("  Select {key}:");
                            if let Some(value) = prompt_select(&prompt, values)? {
                                new_props.insert((*key).clone(), value);
                                changed = true;
                            }
                        }

                        if changed {
                            client.patch_event_properties(&calendar_id, event_id, &new_props).await?;
                            eprintln!("  updated\n");
                            updated += 1;
                        } else {
                            eprintln!("  no changes\n");
                            skipped += 1;
                        }
                    }
                }

                if skipped_no_id > 0 {
                    eprintln!("Warning: skipped {skipped_no_id} event(s) with no ID");
                }
                if !cli.quiet { println!("Done. {updated} updated, {skipped} skipped."); }
            }
            PropertiesAction::Check(args) => {
                let properties = config.properties.as_ref()
                    .context("no [properties] section in config.toml")?;
                if properties.is_empty() {
                    bail!("no properties defined in [properties] section of config.toml");
                }

                let (_, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
                if events.is_empty() {
                    if !cli.quiet { println!("No events found."); }
                    return Ok(());
                }

                let mut issues = 0u32;
                for event in &events {
                    let summary = event.summary_or_default();
                    let start = event.start_str();
                    let shared = event.extended_properties.as_ref().and_then(|p| p.shared.as_ref());

                    let mut event_issues: Vec<String> = Vec::new();

                    for key in properties.keys() {
                        match shared.and_then(|s| s.get(key)) {
                            None => event_issues.push(format!("missing property '{key}'")),
                            Some(value) => {
                                let allowed = &properties[key];
                                if !allowed.contains(value) {
                                    event_issues.push(format!(
                                        "property '{key}' has value '{value}' which is not in allowed values: {}",
                                        allowed.join(", ")
                                    ));
                                }
                            }
                        }
                    }

                    if let Some(shared) = shared {
                        for key in shared.keys() {
                            if !properties.contains_key(key) {
                                event_issues.push(format!("unknown property '{key}'"));
                            }
                        }
                    }

                    if !event_issues.is_empty() {
                        println!("{summary} ({start}):");
                        for issue in &event_issues {
                            println!("  - {issue}");
                        }
                        issues += event_issues.len() as u32;
                    }
                }

                if !cli.quiet {
                    if issues == 0 {
                        println!("All {} event(s) have valid properties.", events.len());
                    } else {
                        println!("\n{issues} issue(s) found across {} event(s).", events.len());
                    }
                }
            }
            PropertiesAction::Delete(args) => {
                let (calendar_id, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
                if events.is_empty() {
                    if !cli.quiet { println!("No events found."); }
                    return Ok(());
                }

                let mut updated = 0u32;
                let mut skipped = 0u32;
                let mut skipped_no_id = 0u32;
                for event in &events {
                    let summary = event.summary_or_default();
                    let start = event.start_str();
                    let event_id = match &event.id {
                        Some(id) => id,
                        None => { skipped_no_id += 1; continue; }
                    };

                    let existing = event.shared_properties();
                    if !existing.contains_key(&args.key) {
                        skipped += 1;
                        continue;
                    }

                    if !args.all {
                        let current_value = &existing[&args.key];
                        let prompt = format!("Delete {}={current_value} from '{summary}' ({start})?", args.key);
                        match prompt_yes_no_quit(&prompt)? {
                            Some(true) => {}
                            Some(false) => { skipped += 1; continue; }
                            None => {
                                if !cli.quiet { println!("\nQuit. {updated} deleted, {skipped} skipped."); }
                                return Ok(());
                            }
                        }
                    }

                    client.delete_property(&calendar_id, event_id, &args.key).await?;
                    if !cli.quiet { println!("  deleted from: {summary} ({start})"); }
                    updated += 1;
                }

                if skipped_no_id > 0 {
                    eprintln!("Warning: skipped {skipped_no_id} event(s) with no ID");
                }
                if !cli.quiet { println!("\nDone. {updated} deleted, {skipped} skipped."); }
            }
            PropertiesAction::Rename(args) => {
                let (calendar_id, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
                if events.is_empty() {
                    if !cli.quiet { println!("No events found."); }
                    return Ok(());
                }

                let mut updated = 0u32;
                let mut skipped = 0u32;
                let mut skipped_no_id = 0u32;
                for event in &events {
                    let summary = event.summary_or_default();
                    let start = event.start_str();
                    let event_id = match &event.id {
                        Some(id) => id,
                        None => { skipped_no_id += 1; continue; }
                    };

                    let mut existing = event.shared_properties();
                    let value = match existing.remove(&args.from) {
                        Some(v) => v,
                        None => { skipped += 1; continue; }
                    };

                    if !args.all {
                        let prompt = format!(
                            "Rename '{}'='{}' to '{}'='{}' on '{summary}' ({start})?",
                            args.from, value, args.to, value
                        );
                        match prompt_yes_no_quit(&prompt)? {
                            Some(true) => {}
                            Some(false) => { skipped += 1; continue; }
                            None => {
                                if !cli.quiet { println!("\nQuit. {updated} renamed, {skipped} skipped."); }
                                return Ok(());
                            }
                        }
                    }

                    existing.insert(args.to.clone(), value);
                    client.patch_event_properties(&calendar_id, event_id, &existing).await?;
                    if !cli.quiet { println!("  renamed on: {summary} ({start})"); }
                    updated += 1;
                }

                if skipped_no_id > 0 {
                    eprintln!("Warning: skipped {skipped_no_id} event(s) with no ID");
                }
                if !cli.quiet { println!("\nDone. {updated} renamed, {skipped} skipped."); }
            }
            PropertiesAction::Edit(args) => {
                let properties = config.properties.as_ref()
                    .context("no [properties] section in config.toml")?;
                if properties.is_empty() {
                    bail!("no properties defined in [properties] section of config.toml");
                }

                let (calendar_id, events) = fetch_events(&client, args.calendar_name.as_deref(), &config).await?;
                if events.is_empty() {
                    if !cli.quiet { println!("No events found."); }
                    return Ok(());
                }

                let sorted_keys: Vec<&String> = {
                    let mut keys: Vec<_> = properties.keys().collect();
                    keys.sort();
                    keys
                };

                let mut updated = 0u32;
                let mut skipped_no_id = 0u32;
                for event in &events {
                    let summary = event.summary_or_default();
                    let start = event.start_str();
                    let end = event.end_str();
                    let event_id = match &event.id {
                        Some(id) => id,
                        None => { skipped_no_id += 1; continue; }
                    };

                    let mut current = event.shared_properties();
                    let mut changed = false;
                    let mut deleted_keys: Vec<String> = Vec::new();

                    loop {
                        eprintln!();
                        eprintln!("{summary}");
                        eprintln!("  start: {start}");
                        eprintln!("  end:   {end}");
                        if let Some(location) = &event.location {
                            eprintln!("  location: {location}");
                        }
                        if let Some(description) = &event.description {
                            eprintln!("  description: {description}");
                        }
                        if current.is_empty() && deleted_keys.is_empty() {
                            eprintln!("  (no properties)");
                        } else {
                            for key in &sorted_keys {
                                if let Some(val) = current.get(*key) {
                                    eprintln!("  {key}: {val}");
                                }
                            }
                            for (k, v) in &current {
                                if !properties.contains_key(k) {
                                    eprintln!("  {k}: {v} (unknown)");
                                }
                            }
                        }

                        struct MenuEntry { key: String, actions: Vec<(&'static str, &'static str)> }
                        let mut menu_entries: Vec<MenuEntry> = Vec::new();

                        for key in &sorted_keys {
                            if current.contains_key(*key) {
                                menu_entries.push(MenuEntry {
                                    key: (*key).clone(),
                                    actions: vec![("c", "change"), ("d", "delete")],
                                });
                            } else {
                                menu_entries.push(MenuEntry {
                                    key: (*key).clone(),
                                    actions: vec![("a", "add")],
                                });
                            }
                        }

                        eprintln!("  Actions:");
                        for (i, entry) in menu_entries.iter().enumerate() {
                            let actions_str: Vec<String> = entry.actions.iter()
                                .map(|(code, label)| format!("{code}={label}"))
                                .collect();
                            eprintln!("    {}: '{}' [{}]", i + 1, entry.key, actions_str.join(", "));
                        }
                        eprintln!("    n: next event");
                        eprintln!("    q: quit");

                        use std::io::{BufRead, Write};
                        eprint!("  choice: ");
                        std::io::stderr().flush()?;
                        let mut line = String::new();
                        std::io::stdin().lock().read_line(&mut line)?;
                        let trimmed = line.trim().to_lowercase();

                        if trimmed == "n" || trimmed == "next" {
                            break;
                        }
                        if trimmed == "q" || trimmed == "quit" {
                            if changed {
                                client.patch_event_properties_with_deletes(&calendar_id, event_id, &current, &deleted_keys).await?;
                                updated += 1;
                                eprintln!("  saved.");
                            }
                            if !cli.quiet { println!("\nQuit. {updated} event(s) updated."); }
                            return Ok(());
                        }

                        let (num_str, action_code) = if trimmed.len() >= 2 && trimmed.as_bytes().last().unwrap().is_ascii_alphabetic() {
                            (&trimmed[..trimmed.len()-1], Some(&trimmed[trimmed.len()-1..]))
                        } else {
                            (trimmed.as_str(), None)
                        };

                        let idx = match num_str.parse::<usize>() {
                            Ok(n) if n >= 1 && n <= menu_entries.len() => n - 1,
                            _ => { eprintln!("  invalid choice"); continue; }
                        };

                        let entry = &menu_entries[idx];
                        let action = if entry.actions.len() == 1 {
                            entry.actions[0].0
                        } else if let Some(code) = action_code {
                            if let Some((a, _)) = entry.actions.iter().find(|(c, _)| *c == code) {
                                a
                            } else {
                                eprintln!("  invalid action. Use: {}", entry.actions.iter().map(|(c, l)| format!("{c}={l}")).collect::<Vec<_>>().join(", "));
                                continue;
                            }
                        } else {
                            eprintln!("  specify action: {}", entry.actions.iter().map(|(c, l)| format!("{c}={l}")).collect::<Vec<_>>().join(", "));
                            continue;
                        };

                        let key = &entry.key;
                        match action {
                            "a" | "c" => {
                                let values = &properties[key];
                                let prompt = format!("  Select value for '{key}':");
                                if let Some(value) = prompt_select(&prompt, values)? {
                                    current.insert(key.clone(), value);
                                    changed = true;
                                }
                            }
                            "d" => {
                                current.remove(key);
                                deleted_keys.push(key.clone());
                                changed = true;
                                eprintln!("  deleted '{key}'");
                            }
                            _ => unreachable!(),
                        }
                    }

                    if changed {
                        client.patch_event_properties_with_deletes(&calendar_id, event_id, &current, &deleted_keys).await?;
                        eprintln!("  saved.");
                        updated += 1;
                    }
                }

                if skipped_no_id > 0 {
                    eprintln!("Warning: skipped {skipped_no_id} event(s) with no ID");
                }
                if !cli.quiet { println!("\nDone. {updated} event(s) updated."); }
            }
        },
        Command::Calendar { action } => match action {
            CalendarAction::Create { name } => {
                let calendar = client.create_calendar(&name).await?;
                let id = calendar["id"].as_str().unwrap_or("<unknown>");
                if !cli.quiet {
                    println!("Created public calendar '{name}'");
                    println!("  id: {id}");
                }
            }
        },
        Command::MoveEvents(args) => {
            let calendars = client.list_calendars().await?;

            let source_cal = calendars.iter()
                .find(|c| c.summary.as_deref() == Some(&args.source))
                .context(format!("no calendar named '{}' found", args.source))?;
            let source_id = source_cal.id.as_deref().context("source calendar has no id")?.to_string();

            let target_cal = calendars.iter()
                .find(|c| c.summary.as_deref() == Some(&args.target))
                .context(format!("no calendar named '{}' found", args.target))?;
            let target_id = target_cal.id.as_deref().context("target calendar has no id")?.to_string();

            if !cli.quiet { println!("Moving events from '{}' to '{}'", args.source, args.target); }
            if !cli.quiet {
                if !args.all {
                    println!("  interactive mode: y=move, n=skip, q=quit");
                }
                println!();
            }

            let events = client.list_all_events(&source_id).await?;
            if events.is_empty() {
                if !cli.quiet { println!("No events found in '{}'.", args.source); }
                return Ok(());
            }

            if !cli.quiet { println!("Found {} event(s)\n", events.len()); }

            let mut moved = 0u32;
            let mut skipped = 0u32;
            for event in &events {
                let summary = event.summary_or_default();
                let start = event.start_str();
                let event_id = match &event.id {
                    Some(id) => id,
                    None => {
                        eprintln!("  skipping event with no id: {summary}");
                        skipped += 1;
                        continue;
                    }
                };

                if !args.all {
                    let prompt = format!("  Move '{summary}' ({start})?");
                    match prompt_yes_no_quit(&prompt)? {
                        Some(true) => {}
                        Some(false) => { skipped += 1; continue; }
                        None => {
                            if !cli.quiet { println!("\nQuit. {moved} event(s) moved, {skipped} skipped."); }
                            return Ok(());
                        }
                    }
                }

                if args.dry_run {
                    if !cli.quiet { println!("  [dry-run] would move: {summary} ({start})"); }
                    moved += 1;
                } else {
                    let mut payload = json!({ "summary": summary });
                    if let Some(start) = &event.start {
                        payload["start"] = serde_json::to_value(start)?;
                    }
                    if let Some(end) = &event.end {
                        payload["end"] = serde_json::to_value(end)?;
                    }
                    if let Some(desc) = &event.description {
                        payload["description"] = json!(desc);
                    }
                    if let Some(loc) = &event.location {
                        payload["location"] = json!(loc);
                    }
                    if let Some(props) = &event.extended_properties {
                        payload["extendedProperties"] = serde_json::to_value(props)?;
                    }

                    client.insert_event_raw(&target_id, &payload).await?;
                    client.delete_event(&source_id, event_id).await?;
                    if !cli.quiet { println!("  moved: {summary} ({start})"); }
                    moved += 1;
                }
            }

            if !cli.quiet {
                if args.dry_run {
                    println!("\nDry run complete. {moved} event(s) would be moved, {skipped} skipped.");
                } else {
                    println!("\nDone. {moved} event(s) moved to '{}', {skipped} skipped.", args.target);
                }
            }
        }
        Command::Auth(_) | Command::Complete { .. } | Command::Defconfig | Command::Version => unreachable!(),
    }

    Ok(())
}