static USAGE: &str = r#"
Explodes a row into multiple ones by splitting a column value based on the
given separator.

Example:

```csv
name,colors
John,blue|yellow
Mary,red
```

# Can be exploded on the "colors" <column> based on the "|" <separator>
$ qsv explode colors "|" data.csv

```csv
name,colors
John,blue
John,yellow
Mary,red
```

See also https://github.com/dathere/qsv/wiki/Transform-and-Reshape#explode

Usage:
    qsv explode [options] <column> <separator> [<input>]
    qsv explode --help

explode options:
    -r, --rename <name>    New name for the exploded column.

Common options:
    -h, --help             Display this message
    -o, --output <file>    Write output to <file> instead of stdout.
    -n, --no-headers       When set, the first row will not be interpreted
                           as headers.
    -d, --delimiter <arg>  The field delimiter for reading CSV data.
                           Must be a single character. (default: ,)
"#;

use serde::Deserialize;

use crate::{
    CliResult,
    config::{Config, Delimiter},
    select::SelectColumns,
    util,
    util::replace_column_value,
};
#[derive(Deserialize)]
struct Args {
    arg_column:      SelectColumns,
    arg_separator:   String,
    arg_input:       Option<String>,
    flag_rename:     Option<String>,
    flag_output:     Option<String>,
    flag_no_headers: bool,
    flag_delimiter:  Option<Delimiter>,
}

pub fn run(argv: &[&str]) -> CliResult<()> {
    let args: Args = util::get_args(USAGE, argv)?;
    let rconfig = Config::new(args.arg_input.as_ref())
        .delimiter(args.flag_delimiter)
        .no_headers_flag(args.flag_no_headers)
        .select(args.arg_column);

    if args.arg_separator.is_empty() {
        return fail_incorrectusage_clierror!("<separator> cannot be empty.");
    }

    let mut rdr = rconfig.reader()?;
    let mut wtr = Config::new(args.flag_output.as_ref()).writer()?;

    let headers = rdr.byte_headers()?.clone();
    let sel = rconfig.selection(&headers)?;
    if sel.len() != 1 {
        return fail_incorrectusage_clierror!(
            "explode requires exactly one <column>; got {} columns.",
            sel.len()
        );
    }
    let column_index = *sel
        .iter()
        .next()
        .expect("explode requires exactly one <column>");

    let mut headers = rdr.headers()?.clone();

    if let Some(new_name) = args.flag_rename {
        headers = replace_column_value(&headers, column_index, &new_name);
    }

    if !rconfig.no_headers {
        wtr.write_record(&headers)?;
    }

    let mut record = csv::StringRecord::new();

    while rdr.read_record(&mut record)? {
        for val in record[column_index].split(&args.arg_separator) {
            let new_record = replace_column_value(&record, column_index, val);
            wtr.write_record(&new_record)?;
        }
    }

    Ok(wtr.flush()?)
}
