popgetter-cli 0.2.2

CLI for popgetter
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
use std::{fs::File, path::Path};
use std::{io, process};

use anyhow::Context;
use clap::{command, Args, Parser, Subcommand};
use enum_dispatch::enum_dispatch;
use indoc::indoc;
use log::{debug, info};
use nonempty::nonempty;
use polars::frame::DataFrame;
use popgetter_core::{
    config::Config,
    data_request_spec::{DataRequestSpec, RegionSpec},
    formatters::{
        CSVFormatter, GeoJSONFormatter, GeoJSONSeqFormatter, OutputFormatter, OutputGenerator,
    },
    geo::BBox,
    search::{
        CaseSensitivity, Country, DataPublisher, DownloadParams, GeometryLevel, MatchType,
        MetricId, Params, SearchConfig, SearchContext, SearchParams, SearchText, SourceDataRelease,
        SourceDownloadUrl, SourceMetricId, YearRange,
    },
    Popgetter,
};
use serde::{Deserialize, Serialize};
use spinners::{Spinner, Spinners};
use strum_macros::EnumString;

#[cfg(feature = "llm")]
mod llm_imports {
    pub use itertools::Itertools;
    pub use langchain_rust::vectorstore::qdrant::{Qdrant, StoreBuilder};
    pub use polars::prelude::*;
    pub use polars::series::Series;
    pub use popgetter_core::{search::SearchResults, COL};
    pub use popgetter_llm::{
        chain::{generate_recipe, generate_recipe_from_results, SYSTEM_PROMPT_1, SYSTEM_PROMPT_2},
        embedding::{init_embeddings, query_embeddings},
        utils::{api_key, azure_open_ai_embedding, serialize_to_json},
    };
    pub use qdrant_client::qdrant::{Condition, Filter};
}
#[cfg(feature = "llm")]
use llm_imports::*;

use crate::display::display_search_results;
use crate::display::{
    display_column, display_column_unique, display_countries, display_metdata_columns,
    display_summary,
};
use crate::error::PopgetterCliResult;

const DEFAULT_PROGRESS_SPINNER: Spinners = Spinners::Dots;
const COMPLETE_PROGRESS_STRING: &str = "✔";
const RUNNING_TAIL_STRING: &str = "...";
const DOWNLOADING_SEARCHING_STRING: &str = "Downloading and searching metadata";

/// Defines the output formats we are able to produce data in.
#[derive(Clone, Debug, Deserialize, Serialize, EnumString, PartialEq, Eq)]
#[strum(ascii_case_insensitive)]
pub enum OutputFormat {
    GeoJSON,
    GeoJSONSeq,
    Csv,
    GeoParquet,
    FlatGeobuf,
    Stdout,
}

fn write_output<T, U>(
    output_generator: T,
    mut data: DataFrame,
    output_file: Option<U>,
) -> PopgetterCliResult<()>
where
    T: OutputGenerator,
    U: AsRef<Path>,
{
    if let Some(output_file) = output_file {
        let mut f = File::create(output_file).context("Failed to write output")?;
        output_generator.save(&mut f, &mut data)?;
    } else {
        let mut stdout_lock = std::io::stdout().lock();
        output_generator.save(&mut stdout_lock, &mut data)?;
    };
    Ok(())
}

/// Trait that defines what to run when a given subcommand is invoked.
#[enum_dispatch]
pub trait RunCommand {
    async fn run(&self, config: Config) -> PopgetterCliResult<()>;
}

/// The `data` command downloads and outputs metrics for a given region in a given format.
#[derive(Args, Debug)]
pub struct DataCommand {
    #[arg(
        short = 'f',
        long,
        value_name = "geojson|geojsonseq|csv",
        help = "Output format for the results"
    )]
    output_format: OutputFormat,
    #[arg(short = 'o', long, help = "Output file to place the results")]
    output_file: Option<String>,
    #[command(flatten)]
    search_params_args: SearchParamsArgs,
    #[command(flatten)]
    download_params_args: DownloadParamsArgs,
    #[arg(
        short = 'r',
        long,
        default_value_t = false,
        help = "Force run without prompt"
    )]
    force_run: bool,
    #[arg(from_global)]
    quiet: bool,
}

#[derive(Args, Debug, Clone)]
struct DownloadParamsArgs {
    #[arg(
        long = "no-geometry",
        help = "When set, no geometry data is included in the results"
    )]
    no_geometry: bool,
}

/// A type combining both the `SearchParamsArgs` and `DownloadParamsArgs` to enable `DownloadParams`
/// to be constructed since fields of `SearchParamsArgs` may also be needed for `DownloadParams`.
#[derive(Clone, Debug)]
struct CombinedParamsArgs {
    search_params_args: SearchParamsArgs,
    download_params_args: DownloadParamsArgs,
}

impl From<CombinedParamsArgs> for DownloadParams {
    fn from(combined_params_args: CombinedParamsArgs) -> Self {
        Self {
            region_spec: combined_params_args
                .search_params_args
                .bbox
                .map(|bbox| vec![RegionSpec::BoundingBox(bbox)])
                .unwrap_or_default(),
            include_geoms: !combined_params_args.download_params_args.no_geometry,
        }
    }
}

impl From<&OutputFormat> for OutputFormatter {
    fn from(value: &OutputFormat) -> Self {
        match value {
            OutputFormat::GeoJSON => OutputFormatter::GeoJSON(GeoJSONFormatter),
            OutputFormat::Csv => OutputFormatter::Csv(CSVFormatter::default()),
            OutputFormat::GeoJSONSeq => OutputFormatter::GeoJSONSeq(GeoJSONSeqFormatter),
            OutputFormat::Stdout => OutputFormatter::Csv(CSVFormatter::default()),
            _ => todo!("output format not implemented"),
        }
    }
}

impl From<OutputFormat> for OutputFormatter {
    fn from(value: OutputFormat) -> Self {
        Self::from(&value)
    }
}

impl RunCommand for DataCommand {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        info!("Running `data` subcommand");
        let sp = (!self.quiet).then(|| {
            Spinner::with_timer(
                DEFAULT_PROGRESS_SPINNER,
                DOWNLOADING_SEARCHING_STRING.to_string() + RUNNING_TAIL_STRING,
            )
        });
        let popgetter = Popgetter::new_with_config_and_cache(config).await?;
        let search_params: SearchParams = self.search_params_args.clone().into();
        let search_results = popgetter.search(&search_params);

        // sp.stop_and_persist is potentially a better method, but not obvious how to
        // store the timing. Leaving below until that option is ruled out.
        // sp.stop_and_persist(&COMPLETE_PROGRESS_STRING, spinner_message.into());
        if let Some(mut s) = sp {
            s.stop_with_symbol(COMPLETE_PROGRESS_STRING)
        }

        let len_requests = search_results.0.shape().0;
        print_metrics_count(len_requests);
        let download_params: DownloadParams = CombinedParamsArgs {
            search_params_args: self.search_params_args.clone(),
            download_params_args: self.download_params_args.clone(),
        }
        .into();

        if !self.force_run {
            println!("Input 'r' to run query, any other character will cancel");
            let mut input = String::new();
            io::stdin().read_line(&mut input).unwrap();
            let input = input.trim().to_lowercase();
            match input.as_str() {
                "r" | "run" | "y" | "yes" => {}
                _ => {
                    println!("Cancelling query.");
                    process::exit(0);
                }
            }
        }
        let sp = (!self.quiet).then(|| {
            Spinner::with_timer(
                DEFAULT_PROGRESS_SPINNER,
                "Downloading metrics".to_string() + RUNNING_TAIL_STRING,
            )
        });
        let data = search_results
            .download(&popgetter.config, &download_params)
            .await?;
        if let Some(mut s) = sp {
            s.stop_with_symbol(COMPLETE_PROGRESS_STRING);
        }
        debug!("{data:#?}");

        let formatter: OutputFormatter = (&self.output_format).into();
        write_output(formatter, data, self.output_file.as_deref())?;
        Ok(())
    }
}

/// The Metrics command allows a user to search for a set of metrics by bounding box and filter.
/// The set of ways to search will likley increase over time
#[derive(Args, Debug)]
pub struct MetricsCommand {
    #[command(flatten)]
    search_params_args: SearchParamsArgs,
    // TODO: consider refactoring SummaryOptions into a separate subcommand so that
    #[clap(flatten)]
    summary_options: SummaryOptions,
    #[clap(flatten)]
    metrics_results_options: MetricsResultsOptions,
    #[arg(from_global)]
    quiet: bool,
}

#[derive(Debug, Args)]
#[group(required = false, multiple = false)]
pub struct SummaryOptions {
    #[arg(long, help = "Summarise results with count of unique values by field")]
    summary: bool,
    #[arg(long, help = "Unique values of a column", value_name = "COLUMN NAME")]
    unique: Option<String>,
    #[arg(long, help = "Values of a column", value_name = "COLUMN NAME")]
    column: Option<String>,
    #[arg(long, help = "Print columns of metadata")]
    display_metadata_columns: bool,
}

#[derive(Debug, Args)]
#[group(required = false, multiple = true)]
pub struct MetricsResultsOptions {
    #[arg(
        short,
        long,
        help = "Show all metrics even if there are a large number"
    )]
    full: bool,
    #[arg(long, help = "Exclude description from search results")]
    exclude_description: bool,
}

#[derive(Debug, Clone, clap::ValueEnum, Copy)]
enum MatchTypeArgs {
    Regex,
    Exact,
}

impl From<MatchTypeArgs> for MatchType {
    fn from(value: MatchTypeArgs) -> Self {
        match value {
            MatchTypeArgs::Exact => MatchType::Exact,
            MatchTypeArgs::Regex => MatchType::Regex,
        }
    }
}

#[derive(Debug, Clone, clap::ValueEnum, Copy)]
enum CaseSensitivityArgs {
    Sensitive,
    Insensitive,
}

impl From<CaseSensitivityArgs> for CaseSensitivity {
    fn from(value: CaseSensitivityArgs) -> Self {
        match value {
            CaseSensitivityArgs::Insensitive => CaseSensitivity::Insensitive,
            CaseSensitivityArgs::Sensitive => CaseSensitivity::Sensitive,
        }
    }
}

/// These are the command-line arguments that can be parsed into a SearchParams. The type is
/// slightly different because of the way we allow people to search in text fields.
#[derive(Args, Debug, Clone)]
pub struct SearchParamsArgs {
    // Note: using `std::vec::Vec` rather than just `Vec`, to enforce that multiple year ranges are
    // passed in a single argument e.g. `-y 2014...2016,2018...2019` rather than multiple arguments
    // e.g. `-y 2014...2016 -y 2018...2019`. See https://github.com/clap-rs/clap/issues/4626 and
    // https://docs.rs/clap/latest/clap/_derive/index.html#arg-types
    #[arg(
        short,
        long,
        help = "\
            Filter by year ranges. All ranges are inclusive; multiple ranges can be\n\
            comma-separated.",
        value_name = "YEAR|START...|...END|START...END",
        value_parser = parse_year_range,
    )]
    year_range: Option<std::vec::Vec<YearRange>>,
    #[arg(short, long, help = "Filter by geometry level")]
    geometry_level: Option<String>,
    #[arg(short, long, help = "Filter by source data release name")]
    source_data_release: Option<String>,
    #[arg(short, long, help = "Filter by data publisher name")]
    publisher: Option<String>,
    #[arg(short, long, help = "Filter by country")]
    country: Option<String>,
    #[arg(
        long,
        help = "\
            Filter by source metric ID (i.e. the name of the table in the original data\n\
            release)."
    )]
    source_metric_id: Option<String>,
    #[arg(long, help = "Filter by source download URL")]
    source_download_url: Option<String>,
    #[arg(
        short = 'i',
        long,
        help = "Specify a metric by its popgetter ID (or a prefix thereof)"
    )]
    id: Vec<String>,
    // Filters for text
    #[arg(long, help="Filter by HXL tag", num_args=0..)]
    hxl: Vec<String>,
    #[arg(long, help="Filter by metric name", num_args=0..)]
    name: Vec<String>,
    #[arg(long, help="Filter by metric description", num_args=0..)]
    description: Vec<String>,
    #[arg(short, long, help="Filter by HXL tag, name, or description", num_args=0..)]
    text: Vec<String>,
    #[arg(
        short,
        long,
        value_name = "LEFT,BOTTOM,RIGHT,TOP",
        allow_hyphen_values = true,
        help = "\
            Bounding box in which to get the results. The bounding box provided must be in\n\
            the same coordinate system as used in the requested geometry file. For\n\
            example, United States has geometries with latitude and longitude (EPSG:4326),\n\
            Great Britain has geometries with the British National Grid (EPSG:27700),\n\
            Northern Ireland has geometries with the Irish Grid (EPSG:29902), and\n\
            Beligum has geometries with the Belgian Lambert 2008 reference system\n\
            (EPSG:3812)."
    )]
    bbox: Option<BBox>,
    #[arg(
        value_enum,
        short = 'm',
        long,
        value_name = "MATCH_TYPE",
        help = "\
        Type of matching to perform on: 'geometry-level', 'source-data-release',\n\
        'publisher', 'country', 'source-metric-id', 'hxl', 'name', 'description'\n\
        arguments during the search.\n",
        default_value_t=MatchTypeArgs::Exact
    )]
    match_type: MatchTypeArgs,
    #[arg(
        value_enum,
        long,
        value_name = "CASE_SENSITIVITY",
        help = "\
        Type of case sensitivity used in matching on: 'geometry-level',\n\
        'source-data-release', 'publisher', 'country', 'source-metric-id', 'hxl',\n\
        'name', 'description', 'text' arguments during the search.\n",
        default_value_t=CaseSensitivityArgs::Insensitive
    )]
    case_sensitivity: CaseSensitivityArgs,
}

/// LLM
#[cfg(feature = "llm")]
#[derive(Subcommand, Debug)]
pub enum LLMCommands {
    Init(InitArgs),
    Query(Box<QueryArgs>),
}

#[cfg(feature = "llm")]
impl RunCommand for LLMCommands {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        match self {
            LLMCommands::Init(init) => init.run(config).await,
            LLMCommands::Query(query) => query.run(config).await,
        }
    }
}
#[cfg(feature = "llm")]
#[derive(Args, Debug)]
pub struct InitArgs {
    #[arg(long)]
    sample_n: Option<usize>,
    #[arg(long)]
    seed: Option<u64>,
    #[arg(long)]
    skip: Option<usize>,
}

#[cfg(feature = "llm")]
#[derive(Clone, Debug, Deserialize, Serialize, EnumString, PartialEq, Eq)]
#[strum(ascii_case_insensitive)]
enum LLMOutputFormat {
    SearchResults,
    DataRequestSpec,
    SearchResultsToRecipe,
}

#[cfg(feature = "llm")]
impl RunCommand for InitArgs {
    async fn run(&self, _config: Config) -> PopgetterCliResult<()> {
        // Initialize Embedder
        let embedder = azure_open_ai_embedding(&api_key()?);

        // Initialize the qdrant_client::Qdrant
        // Ensure Qdrant is running at localhost, with gRPC port at 6334
        // docker run -p 6334:6334 qdrant/qdrant
        let client = Qdrant::from_url("http://localhost:6334").build().unwrap();

        // Init store
        let mut store = StoreBuilder::new()
            .embedder(embedder)
            .client(client)
            .collection_name("popgetter")
            .build()
            .await
            // TODO: fix unwrap
            .unwrap();

        // Init embeddings
        init_embeddings(&mut store, self.sample_n, self.seed, self.skip).await?;

        Ok(())
    }
}

#[cfg(feature = "llm")]
impl RunCommand for QueryArgs {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        // Initialize Embedder
        let embedder = azure_open_ai_embedding(&api_key()?);

        // Initialize the qdrant_client::Qdrant
        // Ensure Qdrant is running at localhost, with gRPC port at 6334
        // docker run -p 6334:6334 qdrant/qdrant
        let client = Qdrant::from_url("http://localhost:6334").build().unwrap();
        let popgetter = Popgetter::new_with_config_and_cache(config).await?;
        let search_params: SearchParams = self.search_params_args.clone().into();
        // Init store
        let mut store_builder = StoreBuilder::new()
            .embedder(embedder)
            .client(client)
            .collection_name("popgetter");

        // Filtering by metadata values (e.g. country)
        // https://qdrant.tech/documentation/concepts/hybrid-queries/?q=color#re-ranking-with-payload-values
        // Add country as search filter if given
        if let Some(country) = search_params.country.as_ref() {
            let search_filter = Filter::must([Condition::matches(
                "metadata.country",
                country.value.to_string(),
            )]);
            store_builder = store_builder.search_filter(search_filter);
        }
        // TODO: fix unwrap
        let store = store_builder.build().await.unwrap();

        match self.output_format {
            LLMOutputFormat::SearchResults => {
                // TODO: see if we can subset similarity search by metadata values
                let results = query_embeddings(&self.query, self.limit, &store).await?;

                log::info!("Results: {:#?}", results);

                let ids = Series::new(
                    COL::METRIC_ID,
                    results
                        .iter()
                        .map(|doc| {
                            doc.metadata
                                .get(COL::METRIC_ID)
                                .unwrap()
                                .as_str()
                                .unwrap()
                                .to_string()
                        })
                        .collect_vec(),
                );

                // Filter afterwards with `COL::METRIC_ID`
                let results = popgetter
                    .search(&search_params)
                    .0
                    .lazy()
                    .filter(col(COL::METRIC_ID).is_in(lit(ids)))
                    .collect()
                    .unwrap();

                if results.shape().0.eq(&0) {
                    println!("No results found.");
                    return Ok(());
                } else {
                    display_search_results(SearchResults(results), None, false).unwrap();
                }
            }
            LLMOutputFormat::SearchResultsToRecipe => {
                // TODO: see if we can subset similarity search by metadata values
                let results = query_embeddings(&self.query, self.limit, &store).await?;

                let ids = Series::new(
                    COL::METRIC_ID,
                    results
                        .iter()
                        .map(|doc| {
                            doc.metadata
                                .get(COL::METRIC_ID)
                                .unwrap()
                                .as_str()
                                .unwrap()
                                .to_string()
                        })
                        .collect_vec(),
                );

                // Filter afterwards with `COL::METRIC_ID`
                let mut results = popgetter
                    .search(&search_params)
                    .0
                    .lazy()
                    .filter(col(COL::METRIC_ID).is_in(lit(ids)))
                    .collect()
                    .unwrap();

                if results.shape().0.eq(&0) {
                    println!("No results found.");
                    return Ok(());
                } else {
                    display_search_results(SearchResults(results.clone()), None, false).unwrap();
                }

                // Generate full metdata as results, now pass this to the recipe generator
                let results_json = serialize_to_json(&mut results).unwrap();

                let data_request_spec =
                    generate_recipe_from_results(&results_json, SYSTEM_PROMPT_2).await?;
                println!("Recipe:\n{:#?}", data_request_spec);
            }
            LLMOutputFormat::DataRequestSpec => {
                let data_request_spec = generate_recipe(
                    &self.query,
                    SYSTEM_PROMPT_1,
                    &store,
                    &popgetter,
                    self.limit,
                    // TODO: uses human readable name to generate metric text, update to config
                    false,
                )
                .await?;
                log::info!("Deserialized recipe:");
                log::info!("{:#?}", data_request_spec);

                // log::info!("Downloading data request...");
            }
        }
        Ok(())
    }
}

#[cfg(feature = "llm")]
#[derive(Args, Debug)]
pub struct QueryArgs {
    #[arg(index = 1)]
    query: String,
    #[arg(long, help = "Number of results to be returned")]
    limit: usize,
    #[command(flatten)]
    search_params_args: SearchParamsArgs,
    #[arg(long, help = "Output format: 'SearchResults' or 'DataRequestSpec'")]
    output_format: LLMOutputFormat,
}

/// Expected behaviour:
/// N.. -> After(N); ..N -> Before(N); M..N -> Between(M, N); N -> Between(N, N)
/// Year ranges can be comma-separated
fn parse_year_range(value: &str) -> anyhow::Result<Vec<YearRange>> {
    value
        .split(',')
        .map(|range| range.parse())
        .collect::<anyhow::Result<Vec<YearRange>>>()
}

// A simple function to manage similaries across multiple cases.
// May ultimately be generalised to a function to manage all progress UX
// that can be switched on and off.
fn print_metrics_count(len_requests: usize) {
    println!("Found {len_requests} metric(s).");
}

fn text_searches_from_args(
    hxl: Vec<String>,
    name: Vec<String>,
    description: Vec<String>,
    text: Vec<String>,
    match_type: MatchType,
    case_sensitivity: CaseSensitivity,
) -> Vec<SearchText> {
    let mut all_text_searches: Vec<SearchText> = vec![];
    all_text_searches.extend(hxl.iter().map(|t| SearchText {
        text: t.clone(),
        context: nonempty![SearchContext::Hxl],
        config: SearchConfig {
            match_type,
            case_sensitivity,
        },
    }));
    all_text_searches.extend(name.iter().map(|t| SearchText {
        text: t.clone(),
        context: nonempty![SearchContext::HumanReadableName],
        config: SearchConfig {
            match_type,
            case_sensitivity,
        },
    }));
    all_text_searches.extend(description.iter().map(|t| SearchText {
        text: t.clone(),
        context: nonempty![SearchContext::Description],
        config: SearchConfig {
            match_type,
            case_sensitivity,
        },
    }));
    all_text_searches.extend(text.iter().map(|t| SearchText {
        text: t.clone(),
        context: SearchContext::all(),
        config: SearchConfig {
            // Always use regex for "text" since SearchContext::all() includes multiple columns
            match_type: MatchType::Regex,
            case_sensitivity,
        },
    }));
    all_text_searches
}

impl From<SearchParamsArgs> for SearchParams {
    fn from(args: SearchParamsArgs) -> Self {
        SearchParams {
            text: text_searches_from_args(
                args.hxl,
                args.name,
                args.description,
                args.text,
                args.match_type.into(),
                args.case_sensitivity.into(),
            ),
            year_range: args.year_range.clone(),
            geometry_level: args.geometry_level.clone().map(|value| GeometryLevel {
                value,
                config: SearchConfig {
                    match_type: args.match_type.into(),
                    case_sensitivity: args.case_sensitivity.into(),
                },
            }),
            source_data_release: args
                .source_data_release
                .clone()
                .map(|value| SourceDataRelease {
                    value,
                    config: SearchConfig {
                        match_type: args.match_type.into(),
                        case_sensitivity: args.case_sensitivity.into(),
                    },
                }),
            data_publisher: args.publisher.clone().map(|value| DataPublisher {
                value,
                config: SearchConfig {
                    match_type: args.match_type.into(),
                    case_sensitivity: args.case_sensitivity.into(),
                },
            }),
            source_download_url: args.source_download_url.map(|value| SourceDownloadUrl {
                value,
                // Always use regex for source download URL
                config: SearchConfig {
                    match_type: MatchType::Regex,
                    case_sensitivity: CaseSensitivity::Insensitive,
                },
            }),
            country: args.country.clone().map(|value| Country {
                value,
                config: SearchConfig {
                    match_type: args.match_type.into(),
                    case_sensitivity: args.case_sensitivity.into(),
                },
            }),
            source_metric_id: args.source_metric_id.clone().map(|value| SourceMetricId {
                value,
                config: SearchConfig {
                    match_type: args.match_type.into(),
                    case_sensitivity: args.case_sensitivity.into(),
                },
            }),
            metric_id: args
                .id
                .clone()
                .into_iter()
                .map(|value| MetricId {
                    id: value,
                    // SearchConfig always `MatchType::Startswith` and `CaseSensitivity::Insensitive`
                    // for `MetricId`
                    config: SearchConfig {
                        match_type: MatchType::Startswith,
                        case_sensitivity: CaseSensitivity::Insensitive,
                    },
                })
                .collect(),
            region_spec: args
                .bbox
                .map(|bbox| vec![RegionSpec::BoundingBox(bbox)])
                .unwrap_or_default(),
        }
    }
}

impl RunCommand for MetricsCommand {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        info!("Running `metrics` subcommand");
        debug!("{:#?}", self);

        let sp = (!self.quiet).then(|| {
            Spinner::with_timer(
                DEFAULT_PROGRESS_SPINNER,
                DOWNLOADING_SEARCHING_STRING.into(),
            )
        });
        let popgetter = Popgetter::new_with_config_and_cache(config).await?;

        let search_results = popgetter.search(&self.search_params_args.to_owned().into());
        if let Some(mut s) = sp {
            s.stop_with_symbol(COMPLETE_PROGRESS_STRING);
        }
        let len_requests = search_results.0.shape().0;

        // Output options:
        // Display: metadata columns
        if self.summary_options.display_metadata_columns {
            display_metdata_columns(&popgetter.metadata.combined_metric_source_geometry())?;
        // Display: summary
        } else if self.summary_options.summary {
            display_summary(search_results)?;
        // Display: unique
        } else if let Some(column) = self.summary_options.unique.as_ref() {
            display_column_unique(search_results, column)?;
        // Display: column
        } else if let Some(column) = self.summary_options.column.as_ref() {
            display_column(search_results, column)?;
        // Display: metrics results
        } else {
            // MetricsResultsOptions: exclude description
            let display_search_results_fn = if self.metrics_results_options.exclude_description {
                // display_search_results_no_description
                display_search_results
            } else {
                display_search_results
            };
            // MetricsResultsOptions: full
            if len_requests > 50 && !self.metrics_results_options.full {
                print_metrics_count(len_requests);
                display_search_results_fn(
                    search_results,
                    Some(50),
                    self.metrics_results_options.exclude_description,
                )?;
                println!(
                    "{} more results not shown. Use --full to show all results.",
                    len_requests - 50
                );
            } else {
                display_search_results_fn(
                    search_results,
                    None,
                    self.metrics_results_options.exclude_description,
                )?;
            }
        }
        Ok(())
    }
}

/// The Countries command should return information about the various countries we have data for.
/// This could include metrics like the number of surveys / metrics / geographies available for
/// each country.
#[derive(Args, Debug)]
pub struct CountriesCommand {
    #[arg(from_global)]
    quiet: bool,
}

impl RunCommand for CountriesCommand {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        info!("Running `countries` subcommand");
        let sp = (!self.quiet).then(|| {
            let spinner_message = "Downloading countries";
            Spinner::with_timer(
                DEFAULT_PROGRESS_SPINNER,
                spinner_message.to_string() + RUNNING_TAIL_STRING,
            )
        });
        let popgetter = Popgetter::new_with_config_and_cache(config).await?;
        if let Some(mut s) = sp {
            s.stop_with_symbol(COMPLETE_PROGRESS_STRING);
        }
        println!("\nThe following countries are available:");
        display_countries(popgetter.metadata.countries, None)?;
        Ok(())
    }
}

/// The Surveys command should list the various surveys that popgetter has access to and related
/// statistics.
#[derive(Args, Debug)]
pub struct SurveysCommand;

impl RunCommand for SurveysCommand {
    async fn run(&self, _config: Config) -> PopgetterCliResult<()> {
        info!("Running `surveys` subcommand");
        unimplemented!("The `Surveys` subcommand is not implemented for the current release");
    }
}

/// The Recipe command loads a recipe file and generates the output data requested
#[derive(Args, Debug)]
pub struct RecipeCommand {
    #[arg(index = 1)]
    recipe_file: String,
    #[arg(short = 'f', long)]
    output_format: OutputFormat,
    #[arg(short = 'o', long)]
    output_file: Option<String>,
}

impl RunCommand for RecipeCommand {
    async fn run(&self, config: Config) -> PopgetterCliResult<()> {
        let popgetter = Popgetter::new_with_config(config).await?;
        let recipe = std::fs::read_to_string(&self.recipe_file).context(format!(
            "Failed to read recipe from file: {}",
            self.recipe_file
        ))?;
        let data_request: DataRequestSpec = serde_json::from_str(&recipe)?;
        let params: Params = data_request.try_into()?;
        let search_results = popgetter.search(&params.search);
        let data = search_results
            .download(&popgetter.config, &params.download)
            .await?;
        debug!("{data:#?}");
        let formatter: OutputFormatter = (&self.output_format).into();
        write_output(formatter, data, self.output_file.as_deref())?;
        Ok(())
    }
}

/// The entrypoint for the CLI.
#[derive(Parser, Debug)]
#[command(version, about="Popgetter is a tool to quickly get the data you need!", long_about = None, name="popgetter")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,
    #[arg(
        short = 'q',
        long = "quiet",
        help = "\
            Do not print progress bar to stdout. Prompt, results and logs (when `RUST_LOG`\n\
            is set) will still be printed.",
        global = true
    )]
    quiet: bool,
    #[arg(
        long = "dev",
        help = indoc!{"Activate developer mode"},
        global = true
    )]
    pub dev: bool,
    #[arg(
        long,
        help = indoc!{
            "Override config with a specified base path. Default config base path is:
            https://popgetter.blob.core.windows.net/releases/v0.2"
        },
        global = true
    )]
    pub base_path: Option<String>,
}

/// Commands contains the list of subcommands avaliable for use in the CLI.
/// Each command should implmement the RunCommand trait and specify the list
/// of required args for that command.
#[derive(Subcommand, Debug)]
#[enum_dispatch(RunCommand)]
pub enum Commands {
    /// List countries for which data are available
    Countries(CountriesCommand),
    /// Output data for a given region and set of metrics
    Data(DataCommand),
    /// List and filter available metrics. Multiple filters are applied conjunctively, i.e. this
    /// command only returns metrics that match all filters.
    Metrics(MetricsCommand),
    /// Surveys
    Surveys(SurveysCommand),
    /// From recipe
    Recipe(RecipeCommand),
    /// From LLM
    #[cfg(feature = "llm")]
    #[command(subcommand)]
    #[allow(clippy::upper_case_acronyms)]
    LLM(LLMCommands),
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use tempfile::NamedTempFile;

    use super::*;

    #[tokio::test]
    async fn test_recipe_command() {
        let recipe_command = RecipeCommand {
            recipe_file: format!("{}/../test_recipe.json", env!("CARGO_MANIFEST_DIR")),
            output_format: OutputFormat::GeoJSON,
            output_file: Some(
                NamedTempFile::new()
                    .unwrap()
                    .path()
                    .to_string_lossy()
                    .to_string(),
            ),
        };
        let result = recipe_command.run(Config::default()).await;
        assert!(result.is_ok())
    }

    #[test]
    fn test_parse_year_range() {
        assert_eq!(
            parse_year_range("2000").unwrap(),
            vec![YearRange::Between(2000, 2000)]
        );
        assert_eq!(
            parse_year_range("2000...").unwrap(),
            vec![YearRange::After(2000)]
        );
        assert_eq!(
            parse_year_range("...2000").unwrap(),
            vec![YearRange::Before(2000)]
        );
        assert_eq!(
            parse_year_range("2000...2001").unwrap(),
            vec![YearRange::Between(2000, 2001)]
        );
        assert_eq!(
            parse_year_range("2000...2001,2005...").unwrap(),
            vec![YearRange::Between(2000, 2001), YearRange::After(2005)]
        );
        assert_eq!(
            parse_year_range("...2001,2005,2009").unwrap(),
            vec![
                YearRange::Before(2001),
                YearRange::Between(2005, 2005),
                YearRange::Between(2009, 2009)
            ]
        );
    }

    #[test]
    fn output_type_should_deserialize_properly() {
        let output_format = OutputFormat::from_str("GeoJSON");
        assert_eq!(
            output_format.unwrap(),
            OutputFormat::GeoJSON,
            "geojson format should be parsed correctly"
        );
        let output_format = OutputFormat::from_str("GeoJson");
        assert_eq!(
            output_format.unwrap(),
            OutputFormat::GeoJSON,
            "parsing should be case insensitive"
        );
        let output_format = OutputFormat::from_str("geoparquet");
        assert_eq!(
            output_format.unwrap(),
            OutputFormat::GeoParquet,
            "correct variants should parse correctly"
        );
        let output_format = OutputFormat::from_str("awesome_tiny_model");
        assert!(output_format.is_err(), "non listed formats should fail");
    }

    #[test]
    fn cli() {
        use clap::CommandFactory;
        Cli::command().debug_assert();
    }
}