redisctl 0.10.1

Unified CLI for Redis Cloud and Enterprise
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
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
//! Implementation of additional subscription commands

use super::async_utils::{AsyncOperationArgs, handle_async_response};
use super::utils::*;
use crate::cli::OutputFormat;
use crate::connection::ConnectionManager;
use crate::error::{RedisCtlError, Result as CliResult};
use crate::output::print_output;
use anyhow::Context;
use redisctl_core::cloud::delete_subscription_and_wait;
use redisctl_core::progress::ProgressEvent;
use serde_json::{Value, json};
use std::time::Duration;
use tabled::{Table, Tabled, settings::Style};

/// Helper to print non-table output
fn print_json_or_yaml(data: Value, output_format: OutputFormat) -> CliResult<()> {
    match output_format {
        OutputFormat::Json => print_output(data, crate::output::OutputFormat::Json, None)?,
        OutputFormat::Yaml => print_output(data, crate::output::OutputFormat::Yaml, None)?,
        _ => print_output(data, crate::output::OutputFormat::Json, None)?,
    }
    Ok(())
}

/// Read JSON data from string or file
fn read_json_data(data: &str) -> CliResult<Value> {
    let json_str = if let Some(file_path) = data.strip_prefix('@') {
        // Read from file
        std::fs::read_to_string(file_path).map_err(|e| RedisCtlError::InvalidInput {
            message: format!("Failed to read file {}: {}", file_path, e),
        })?
    } else {
        // Use as-is
        data.to_string()
    };

    serde_json::from_str(&json_str).map_err(|e| RedisCtlError::InvalidInput {
        message: format!("Invalid JSON: {}", e),
    })
}

/// Create a new subscription
#[allow(clippy::too_many_arguments)]
pub async fn create_subscription(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    name: Option<&str>,
    dry_run: bool,
    deployment_type: Option<&str>,
    payment_method: &str,
    payment_method_id: Option<i32>,
    memory_storage: &str,
    persistent_storage_encryption: &str,
    data: Option<&str>,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // Validate: cloudProviders and databases are required
    if data.is_none() {
        return Err(RedisCtlError::InvalidInput {
            message: "--data is required for subscription creation (must include cloudProviders and databases arrays). Use first-class parameters (--name, --payment-method, etc.) to override specific values.".to_string(),
        });
    }

    // CLI parameters override JSON values
    if let Some(name_val) = name {
        request_obj.insert("name".to_string(), serde_json::json!(name_val));
    }

    // Always set dry_run if specified (even if false, to be explicit)
    if dry_run {
        request_obj.insert("dryRun".to_string(), serde_json::json!(true));
    }

    if let Some(deployment) = deployment_type {
        request_obj.insert("deploymentType".to_string(), serde_json::json!(deployment));
    }

    // Always set payment method (has default)
    request_obj.insert(
        "paymentMethod".to_string(),
        serde_json::json!(payment_method),
    );

    if let Some(pm_id) = payment_method_id {
        request_obj.insert("paymentMethodId".to_string(), serde_json::json!(pm_id));
    } else if payment_method == "credit-card" && !request_obj.contains_key("paymentMethodId") {
        return Err(RedisCtlError::InvalidInput {
            message: "--payment-method-id is required when using credit-card payment method"
                .to_string(),
        });
    }

    // Always set memory storage (has default)
    request_obj.insert(
        "memoryStorage".to_string(),
        serde_json::json!(memory_storage),
    );

    // Always set persistent storage encryption (has default)
    request_obj.insert(
        "persistentStorageEncryption".to_string(),
        serde_json::json!(persistent_storage_encryption),
    );

    // Validate required nested structures
    if !request_obj.contains_key("cloudProviders") {
        return Err(RedisCtlError::InvalidInput {
            message: "cloudProviders array is required in --data (defines provider, regions, and networking)".to_string(),
        });
    }

    if !request_obj.contains_key("databases") {
        return Err(RedisCtlError::InvalidInput {
            message:
                "databases array is required in --data (at least one database specification needed)"
                    .to_string(),
        });
    }

    let response = client
        .post_raw("/subscriptions", request)
        .await
        .context("Failed to create subscription")?;

    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        async_ops,
        output_format,
        query,
        "Subscription created successfully",
    )
    .await
}

/// Update subscription configuration
#[allow(clippy::too_many_arguments)]
pub async fn update_subscription(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    name: Option<&str>,
    payment_method: Option<&str>,
    payment_method_id: Option<i32>,
    data: Option<&str>,
    dry_run: bool,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(name_val) = name {
        request_obj.insert("name".to_string(), serde_json::json!(name_val));
    }

    if let Some(pm) = payment_method {
        request_obj.insert("paymentMethod".to_string(), serde_json::json!(pm));
    }

    if let Some(pm_id) = payment_method_id {
        request_obj.insert("paymentMethodId".to_string(), serde_json::json!(pm_id));
    }

    // Validate that we have at least one field to update
    if request_obj.is_empty() {
        return Err(RedisCtlError::InvalidInput {
            message: "At least one update field is required (--name, --payment-method, --payment-method-id, or --data)".to_string(),
        });
    }

    if dry_run {
        request_obj.insert("dryRun".to_string(), serde_json::json!(true));
    }

    let response = client
        .put_raw(&format!("/subscriptions/{}", id), request)
        .await
        .context("Failed to update subscription")?;

    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        async_ops,
        output_format,
        query,
        "Subscription updated successfully",
    )
    .await
}

/// Delete a subscription
#[allow(clippy::too_many_arguments)]
pub async fn delete_subscription(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    force: bool,
    dry_run: bool,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    if dry_run {
        eprintln!("Would delete subscription {}.", id);
        eprintln!();
        eprintln!("No changes were made.");
        return Ok(());
    }

    // Confirmation prompt unless --force is used
    if !force {
        use dialoguer::Confirm;
        let confirm = Confirm::new()
            .with_prompt(format!("Are you sure you want to delete subscription {}? This will delete all databases in the subscription!", id))
            .default(false)
            .interact()
            .map_err(|e| RedisCtlError::InvalidInput {
                message: format!("Failed to read confirmation: {}", e),
            })?;

        if !confirm {
            println!("Subscription deletion cancelled");
            return Ok(());
        }
    }

    // Use Layer 2 workflow when --wait is specified
    if async_ops.wait {
        delete_subscription_with_workflow(conn_mgr, profile_name, id, async_ops, output_format)
            .await
    } else {
        delete_subscription_legacy(conn_mgr, profile_name, id, async_ops, output_format, query)
            .await
    }
}

/// Delete subscription using Layer 2 workflow (with --wait)
async fn delete_subscription_with_workflow(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Build progress callback for spinner updates
    let progress_callback: Option<Box<dyn Fn(ProgressEvent) + Send + Sync>> =
        Some(Box::new(|event| {
            if let ProgressEvent::Polling {
                status, elapsed, ..
            } = event
            {
                eprintln!("Status: {} ({:.0}s elapsed)", status, elapsed.as_secs());
            }
        }));

    // Use Layer 2 workflow
    delete_subscription_and_wait(
        &client,
        id as i32,
        Duration::from_secs(async_ops.wait_timeout),
        progress_callback,
    )
    .await
    .context("Failed to delete subscription")?;

    // Output result
    match output_format {
        OutputFormat::Auto | OutputFormat::Table => {
            println!("Subscription {} deleted successfully", id);
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            let result = json!({
                "subscription_id": id,
                "status": "deleted"
            });
            print_json_or_yaml(result, output_format)?;
        }
    }

    Ok(())
}

/// Delete subscription using legacy approach (without --wait)
async fn delete_subscription_legacy(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let response = client
        .delete_raw(&format!("/subscriptions/{}", id))
        .await
        .context("Failed to delete subscription")?;

    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        async_ops,
        output_format,
        query,
        "Subscription deletion initiated",
    )
    .await
}

/// Redis version info for table display
#[derive(Tabled)]
struct RedisVersionRow {
    #[tabled(rename = "VERSION")]
    version: String,
    #[tabled(rename = "RELEASE DATE")]
    release_date: String,
    #[tabled(rename = "END OF LIFE")]
    end_of_life: String,
}

/// Get available Redis versions
pub async fn get_redis_versions(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    subscription_id: Option<u32>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let path = if let Some(sub_id) = subscription_id {
        format!("/redis-versions?subscription={}", sub_id)
    } else {
        "/redis-versions".to_string()
    };

    let response = client
        .get_raw(&path)
        .await
        .context("Failed to get Redis versions")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            let mut rows = Vec::new();

            if let Some(Value::Array(versions)) = result.get("versions") {
                for version in versions {
                    rows.push(RedisVersionRow {
                        version: extract_field(version, "version", ""),
                        release_date: format_date(extract_field(version, "releaseDate", "")),
                        end_of_life: format_date(extract_field(version, "endOfLife", "")),
                    });
                }
            }

            if rows.is_empty() {
                println!("No Redis versions found");
            } else {
                let mut table = Table::new(rows);
                table.with(Style::blank());
                println!("{}", table);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Get subscription pricing
pub async fn get_pricing(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let response = client
        .get_raw(&format!("/subscriptions/{}/pricing", id))
        .await
        .context("Failed to get subscription pricing")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            if let Some(price) = result.get("estimatedMonthlyTotal") {
                println!("Estimated Monthly Total: ${}", price);
            }
            if let Some(currency) = result.get("currency") {
                println!("Currency: {}", currency);
            }
            if let Some(details) = result.get("shards") {
                println!(
                    "Shard Pricing Details: {}",
                    serde_json::to_string_pretty(details)?
                );
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// CIDR entry for table display
#[derive(Tabled)]
struct CidrEntry {
    #[tabled(rename = "CIDR")]
    cidr: String,
    #[tabled(rename = "DESCRIPTION")]
    description: String,
}

/// Get CIDR allowlist
pub async fn get_cidr_allowlist(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let response = client
        .get_raw(&format!("/subscriptions/{}/cidr", id))
        .await
        .context("Failed to get CIDR allowlist")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            let mut entries = Vec::new();

            if let Some(Value::Array(cidrs)) = result.get("cidrs") {
                for cidr in cidrs {
                    entries.push(CidrEntry {
                        cidr: extract_field(cidr, "cidr", ""),
                        description: extract_field(cidr, "description", ""),
                    });
                }
            }

            if entries.is_empty() {
                println!("No CIDR blocks configured");
            } else {
                let mut table = Table::new(entries);
                table.with(Style::blank());
                println!("{}", table);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Update CIDR allowlist
#[allow(clippy::too_many_arguments)]
pub async fn update_cidr_allowlist(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    cidrs: &[String],
    security_groups: &[String],
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // Build cidrIps array from --cidr parameters
    if !cidrs.is_empty() {
        let cidr_ips: Vec<Value> = cidrs
            .iter()
            .map(|cidr| serde_json::json!({ "cidr": cidr }))
            .collect();
        request_obj.insert("cidrIps".to_string(), Value::Array(cidr_ips));
    }

    // Build securityGroupIds array from --security-group parameters
    if !security_groups.is_empty() {
        request_obj.insert(
            "securityGroupIds".to_string(),
            serde_json::json!(security_groups),
        );
    }

    // Validate that we have at least one field to update
    if request_obj.is_empty() {
        return Err(RedisCtlError::InvalidInput {
            message: "At least one update field is required (--cidr, --security-group, or --data)"
                .to_string(),
        });
    }

    let response = client
        .put_raw(&format!("/subscriptions/{}/cidr", id), request)
        .await
        .context("Failed to update CIDR allowlist")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            println!("CIDR allowlist updated successfully");
            if let Some(task_id) = result.get("taskId") {
                println!("Task ID: {}", task_id);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Maintenance window for table display
#[derive(Tabled)]
struct MaintenanceWindowRow {
    #[tabled(rename = "MODE")]
    mode: String,
    #[tabled(rename = "WINDOW")]
    window: String,
}

/// Get maintenance windows
pub async fn get_maintenance_windows(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let response = client
        .get_raw(&format!("/subscriptions/{}/maintenance-windows", id))
        .await
        .context("Failed to get maintenance windows")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            let mut rows = Vec::new();

            if let Some(mode) = result.get("mode") {
                let window_text = if let Some(windows) = result.get("windows") {
                    serde_json::to_string(windows).unwrap_or_else(|_| "N/A".to_string())
                } else {
                    "N/A".to_string()
                };

                rows.push(MaintenanceWindowRow {
                    mode: mode.as_str().unwrap_or("").to_string(),
                    window: window_text,
                });
            }

            if rows.is_empty() {
                println!("No maintenance windows configured");
            } else {
                let mut table = Table::new(rows);
                table.with(Style::blank());
                println!("{}", table);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Update maintenance windows
#[allow(clippy::too_many_arguments)]
pub async fn update_maintenance_windows(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    mode: Option<&str>,
    windows: &[String],
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(mode_val) = mode {
        request_obj.insert("mode".to_string(), serde_json::json!(mode_val));
    }

    // Build windows array from --window parameters
    // Format: "DAY:HH:MM-HH:MM" e.g., "Monday:03:00-07:00"
    if !windows.is_empty() {
        let window_objects: Result<Vec<Value>, _> = windows
            .iter()
            .map(|w| parse_maintenance_window(w))
            .collect();
        match window_objects {
            Ok(objs) => {
                request_obj.insert("windows".to_string(), Value::Array(objs));
            }
            Err(e) => return Err(e),
        }
    }

    // Validate that we have at least one field to update
    if request_obj.is_empty() {
        return Err(RedisCtlError::InvalidInput {
            message: "At least one update field is required (--mode, --window, or --data)"
                .to_string(),
        });
    }

    let response = client
        .put_raw(
            &format!("/subscriptions/{}/maintenance-windows", id),
            request,
        )
        .await
        .context("Failed to update maintenance windows")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            println!("Maintenance windows updated successfully");
            if let Some(task_id) = result.get("taskId") {
                println!("Task ID: {}", task_id);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Parse maintenance window string into JSON object
/// Format: "DAY:HH:MM-HH:MM" or "DAY:HH:MM:DURATION"
fn parse_maintenance_window(window: &str) -> CliResult<Value> {
    let parts: Vec<&str> = window.splitn(2, ':').collect();
    if parts.len() != 2 {
        return Err(RedisCtlError::InvalidInput {
            message: format!(
                "Invalid window format '{}'. Expected 'DAY:HH:MM-HH:MM' (e.g., 'Monday:03:00-07:00')",
                window
            ),
        });
    }

    let day = parts[0];
    let time_part = parts[1];

    // Parse time range (HH:MM-HH:MM) or start time with duration
    if let Some((start, end)) = time_part.split_once('-') {
        Ok(serde_json::json!({
            "dayOfWeek": day,
            "startHour": parse_hour(start)?,
            "endHour": parse_hour(end)?
        }))
    } else {
        // Just start hour provided
        Ok(serde_json::json!({
            "dayOfWeek": day,
            "startHour": parse_hour(time_part)?
        }))
    }
}

/// Parse hour string (HH:MM or HH) to integer hour
fn parse_hour(time: &str) -> CliResult<i32> {
    let hour_str = time.split(':').next().unwrap_or(time);
    hour_str
        .parse::<i32>()
        .map_err(|_| RedisCtlError::InvalidInput {
            message: format!("Invalid hour '{}'. Expected HH or HH:MM format", time),
        })
}

/// Active-Active region for table display
#[derive(Tabled)]
struct AaRegionRow {
    #[tabled(rename = "REGION")]
    region: String,
    #[tabled(rename = "PROVIDER")]
    provider: String,
    #[tabled(rename = "STATUS")]
    status: String,
}

/// List Active-Active regions
pub async fn list_aa_regions(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let response = client
        .get_raw(&format!("/subscriptions/{}/regions", id))
        .await
        .context("Failed to get Active-Active regions")?;

    let result = if let Some(q) = query {
        apply_jmespath(&response, q)?
    } else {
        response
    };

    match output_format {
        OutputFormat::Table => {
            let mut rows = Vec::new();

            if let Some(Value::Array(regions)) = result.get("regions") {
                for region in regions {
                    rows.push(AaRegionRow {
                        region: extract_field(region, "region", ""),
                        provider: extract_field(region, "provider", ""),
                        status: format_status_text(&extract_field(region, "status", "")),
                    });
                }
            }

            if rows.is_empty() {
                println!("No Active-Active regions found");
            } else {
                let mut table = Table::new(rows);
                table.with(Style::blank());
                println!("{}", table);
            }
        }
        _ => print_json_or_yaml(result, output_format)?,
    }

    Ok(())
}

/// Add region to Active-Active subscription
#[allow(clippy::too_many_arguments)]
pub async fn add_aa_region(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    region: Option<&str>,
    deployment_cidr: Option<&str>,
    vpc_id: Option<&str>,
    resp_version: Option<&str>,
    dry_run: bool,
    data: Option<&str>,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(region_val) = region {
        request_obj.insert("region".to_string(), serde_json::json!(region_val));
    }

    if let Some(cidr) = deployment_cidr {
        request_obj.insert("deploymentCIDR".to_string(), serde_json::json!(cidr));
    }

    if let Some(vpc) = vpc_id {
        request_obj.insert("vpcId".to_string(), serde_json::json!(vpc));
    }

    if let Some(resp) = resp_version {
        request_obj.insert("respVersion".to_string(), serde_json::json!(resp));
    }

    if dry_run {
        request_obj.insert("dryRun".to_string(), serde_json::json!(true));
    }

    // Validate that region is provided
    if !request_obj.contains_key("region") {
        return Err(RedisCtlError::InvalidInput {
            message: "--region is required (or provide via --data JSON)".to_string(),
        });
    }

    let response = client
        .post_raw(&format!("/subscriptions/{}/regions", id), request)
        .await
        .context("Failed to add Active-Active region")?;

    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        async_ops,
        output_format,
        query,
        "Active-Active region added successfully",
    )
    .await
}

/// Delete regions from Active-Active subscription
#[allow(clippy::too_many_arguments)]
pub async fn delete_aa_regions(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    regions: &[String],
    dry_run: bool,
    data: Option<&str>,
    force: bool,
    async_ops: &AsyncOperationArgs,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    // Confirmation prompt unless --force is used
    if !force {
        use dialoguer::Confirm;
        let region_list = if regions.is_empty() {
            "specified regions".to_string()
        } else {
            regions.join(", ")
        };
        let confirm = Confirm::new()
            .with_prompt(format!(
                "Are you sure you want to delete regions ({}) from Active-Active subscription {}?",
                region_list, id
            ))
            .default(false)
            .interact()
            .map_err(|e| RedisCtlError::InvalidInput {
                message: format!("Failed to read confirmation: {}", e),
            })?;

        if !confirm {
            println!("Region deletion cancelled");
            return Ok(());
        }
    }

    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut request = if let Some(data_str) = data {
        read_json_data(data_str)?
    } else {
        serde_json::json!({})
    };

    let request_obj = request.as_object_mut().unwrap();

    // Build regions array from --region parameters
    if !regions.is_empty() {
        request_obj.insert("regions".to_string(), serde_json::json!(regions));
    }

    if dry_run {
        request_obj.insert("dryRun".to_string(), serde_json::json!(true));
    }

    // Validate that regions are provided
    if !request_obj.contains_key("regions") {
        return Err(RedisCtlError::InvalidInput {
            message: "At least one --region is required (or provide via --data JSON)".to_string(),
        });
    }

    // Use DELETE with body - need to use post_raw with custom method or adjust API
    // The Redis Cloud API uses DELETE with a request body for this endpoint
    let response = client
        .delete_with_body(&format!("/subscriptions/{}/regions", id), request)
        .await
        .context("Failed to delete Active-Active regions")?;

    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        async_ops,
        output_format,
        query,
        "Active-Active regions deletion initiated",
    )
    .await
}