static USAGE: &str = r#"
Quickly sniff the first n rows and infer CSV metadata (delimiter, header row, number of
preamble rows, quote character, flexible, is_utf8, average record length, number of records,
content length and estimated number of records if sniffing a URL, file size, number of fields,
field names & data types).

`sniff` is also a mime type detector, returning the detected mime type, file size and
last modified date. If --no-infer is enabled, it doesn't even bother to infer the CSV's schema.
This makes it useful for accelerated CKAN harvesting and for checking stale/broken resource URLs.

When qsv is compiled with the optional `magika` feature, it uses Magika - Google's AI-powered
content detection library to identify file types with high accuracy. Magika detects over
200 content types including CSV, parquet, MS Office/Open Document files, JSON, PDF, PNG, JPEG & more.
See https://opensource.googleblog.com/2025/11/announcing-magika-10-now-faster-smarter.html.

When the `magika` feature is not enabled in a build (e.g., MUSL builds, qsvlite, qsvdp), it falls back
to the file-format library which provides basic MIME type detection.

NOTE: This command "sniffs" a CSV's schema by sampling the first n rows (default: 1000)
of a file. Its inferences are sometimes wrong if the the file is too small to infer a pattern
or if the CSV has unusual formatting - with atypical delimiters, quotes, etc.

In such cases, selectively use the --sample, --delimiter and --quote options to improve
the accuracy of the sniffed schema.

If you want more robust, guaranteed schemata, use the "schema" or "stats" commands
instead as they scan the entire file. However, they only work on local files and well-formed
CSVs, unlike `sniff` which can work with remote files, various CSV dialects and is very fast
regardless of file size.

Examples:

  # Sniff a local CSV file
  qsv sniff data.csv

  # Sniff a remote TSV file over HTTPS
  qsv sniff https://example.com/data.tsv

  # Get the mime type of a remote file without inferring the CSV schema
  qsv sniff --no-infer https://example.com/data.xlsx

  # Sniff the first 20 percent of a SSV file
  qsv sniff --sample 0.20 data.ssv

For examples, see https://github.com/dathere/qsv/blob/master/tests/test_sniff.rs.
See also https://github.com/dathere/qsv/wiki/Selection-and-Inspection#sniff

Usage:
    qsv sniff [options] [<input>]
    qsv sniff --help

sniff arguments:
    <input>                  The file to sniff. This can be a local file, stdin or a
                             URL (http and https schemes supported).

                             Note that when input is a URL and is a CSV candidate,
                             sniff will automatically download a sample to a temporary
                             file and sniff it.

                             If the file sample is not a CSV, sniff will return as an error
                             - the detected mime type, file size & last modified date.

                             When --no-infer is enabled, sniff will not bother to infer the
                             CSV schema and just work as a general mime type detector -
                             returning the detected mime type, file size and last modified date.

                             When sniffing a local file, it will scan the file to detect the mime type.
                             When sniffing a URL, it will only download the first chunk of the file if 
                             the --quick option is enabled, otherwise it will download the entire file.
                             
sniff options:
    --sample <size>          First n rows to sample to sniff out the metadata.
                             When sample size is between 0 and 1 exclusive,
                             it is treated as a percentage of the CSV to sample
                             (e.g. 0.20 is 20 percent).
                             When it is zero, the entire file will be sampled.
                             When the input is a URL, the sample size dictates
                             how many lines to sample without having to
                             download the entire file. Ignored when --no-infer is enabled.
                             When sniffing a local file that has a CSV index, the
                             sample budget is instead drawn as a DISTRIBUTED sample
                             (the first & last 5 rows, 5 rows each around the 25th,
                             50th & 75th percentiles, and the rest random across the
                             whole file) rather than just the first n rows. This
                             improves type/date inference for values that only appear
                             late in the file. Run `qsv index` to create an index.
                             [default: 1000]
    --prefer-dmy             Prefer to parse dates in dmy format. Otherwise, use mdy format.
                             Ignored when --no-infer is enabled.
    -d, --delimiter <arg>    The delimiter for reading CSV data.
                             Specify this when the delimiter is known beforehand,
                             as the delimiter inferencing algorithm can sometimes fail.
                             Must be a single ascii character.
    --quote <arg>            The quote character for reading CSV data.
                             Specify this when the quote character is known beforehand,
                             as the quote char inferencing algorithm can sometimes fail.
                             Must be a single ascii character - typically, double quote ("),
                             single quote ('), or backtick (`).
    --json                   Return results in JSON format.
    --pretty-json            Return results in pretty JSON format.
    --save-urlsample <file>  Save the URL sample to a file.
                             Valid only when input is a URL.
    --timeout <secs>         Timeout when sniffing URLs in seconds. If 0, no timeout is used.
                             [default: 30]
    --user-agent <agent>     Specify custom user agent to use when sniffing a CSV on a URL.
                             It supports the following variables - $QSV_VERSION, $QSV_TARGET,
                             $QSV_BIN_NAME, $QSV_KIND and $QSV_COMMAND. Try to follow the syntax here -
                             https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
    --stats-types            Use the same data type names as `stats`.
                             (Unsigned, Signed => Integer, Text => String, everything else the same)
    --no-infer               Do not infer the schema. Only return the file's mime type, size and
                             last modified date. Use this to use sniff as a general mime type detector.
                             Note that CSV and TSV files will only be detected as mime type plain/text
                             in this mode.
    --just-mime              Only return the file's mime type. Use this to use sniff as a general
                             mime type detector. Synonym for --no-infer.
    -Q, --quick              When sniffing a non-CSV remote file, only download the first chunk of the file
                             before attempting to detect the mime type. This is faster but less accurate as
                             some mime types cannot be detected with just the first downloaded chunk.
    --harvest-mode           This is a convenience flag when using sniff in CKAN harvesters. 
                             It is equivalent to --quick --timeout 10 --stats-types --json
                             and --user-agent "CKAN-harvest/$QSV_VERSION ($QSV_TARGET; $QSV_BIN_NAME)"

Common options:
    -h, --help               Display this message
    -p, --progressbar        Show progress bars. Only valid for URL input.
"#;

#[cfg(feature = "magika")]
use std::sync::{LazyLock, Mutex};
use std::{
    cmp::min,
    collections::BTreeSet,
    fmt, fs,
    io::{Seek, SeekFrom, Write, copy},
    path::PathBuf,
    time::Duration,
};

use bytes::Bytes;
use csv_nose::{DatePreference, SampleSize, Sniffer};
#[cfg(not(feature = "magika"))]
use file_format::FileFormat;
use futures::executor::block_on;
use futures_util::StreamExt;
use indicatif::HumanCount;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
use indicatif::{HumanBytes, ProgressBar, ProgressDrawTarget, ProgressStyle};
#[cfg(feature = "magika")]
use magika::Session;
use qsv_tabwriter::TabWriter;
use rand::{RngExt, rngs::StdRng};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tempfile::NamedTempFile;
use url::Url;

use crate::{
    CliResult,
    config::{Config, Delimiter},
    util,
    util::format_systemtime,
};

// Magika ML session - lazy initialized and reused for all detections
#[cfg(feature = "magika")]
static MAGIKA_SESSION: LazyLock<Result<Mutex<Session>, String>> =
    LazyLock::new(|| Session::new().map(Mutex::new).map_err(|e| e.to_string()));

/// Get label from file-format, with special handling for CSV/TSV
#[cfg(not(feature = "magika"))]
fn get_label(fmt: &file_format::FileFormat, mime_type: &str) -> String {
    // Special case for CSV/TSV which file-format doesn't specifically detect
    if mime_type == "text/csv" || mime_type == "application/csv" {
        return "csv".to_string();
    }
    if mime_type == "text/tab-separated-values" {
        return "tsv".to_string();
    }
    // Use short_name if available, otherwise fall back to name
    fmt.short_name()
        .unwrap_or_else(|| fmt.name())
        .to_lowercase()
        .replace(' ', "-")
}

/// Detect mime type from bytes using available backend
/// Returns (`mime_type`, label, `inference_score`)
#[cfg(feature = "magika")]
pub(crate) fn detect_mime_from_bytes(bytes: &[u8]) -> (String, String, f32) {
    // If Magika failed to initialize or the mutex is poisoned, fall back to a default mime type
    let default_mime = || {
        (
            "application/octet-stream".to_string(),
            "unknown".to_string(),
            1.0_f32, // default score for fallback
        )
    };

    match &*MAGIKA_SESSION {
        Ok(mutex)
            if let Ok(mut session) = mutex.lock()
                && let Ok(result) = session.identify_content_sync(bytes) =>
        {
            (
                result.info().mime_type.to_string(),
                result.info().label.to_string(),
                result.score(),
            )
        },
        _ => default_mime(),
    }
}

#[cfg(not(feature = "magika"))]
pub(crate) fn detect_mime_from_bytes(bytes: &[u8]) -> (String, String, f32) {
    let fmt = FileFormat::from_bytes(bytes);
    let mime = fmt.media_type().to_string();
    let label = get_label(&fmt, &mime);
    (mime, label, 1.0)
}

/// Detect mime type from file using available backend
/// Returns (`mime_type`, label, `inference_score`)
#[cfg(feature = "magika")]
fn detect_mime_from_file(path: &std::path::Path) -> Result<(String, String, f32), String> {
    match &*MAGIKA_SESSION {
        Ok(mutex) => mutex
            .lock()
            .map_err(|_| "Magika session mutex poisoned".to_string())?
            .identify_file_sync(path)
            .map(|r| {
                (
                    r.info().mime_type.to_string(),
                    r.info().label.to_string(),
                    r.score(),
                )
            })
            .map_err(|e| format!("Magika detection error: {e}")),
        Err(err) => Err(format!("Magika initialization error: {err}")),
    }
}

#[cfg(not(feature = "magika"))]
fn detect_mime_from_file(path: &std::path::Path) -> Result<(String, String, f32), String> {
    FileFormat::from_file(path)
        .map(|fmt| {
            let mime = fmt.media_type().to_string();
            let label = get_label(&fmt, &mime);
            (mime, label, 1.0)
        })
        .map_err(|e| format!("File format detection error: {e}"))
}

#[allow(dead_code)]
#[derive(Deserialize)]
struct Args {
    arg_input:           Option<String>,
    flag_sample:         f64,
    flag_prefer_dmy:     bool,
    flag_json:           bool,
    flag_save_urlsample: Option<String>,
    flag_pretty_json:    bool,
    flag_delimiter:      Option<Delimiter>,
    flag_quote:          Option<char>,
    flag_progressbar:    bool,
    flag_timeout:        u16,
    flag_user_agent:     Option<String>,
    flag_stats_types:    bool,
    flag_no_infer:       bool,
    flag_just_mime:      bool,
    flag_quick:          bool,
    flag_harvest_mode:   bool,
}

#[derive(Serialize, Deserialize, Default, Debug)]
struct SniffStruct {
    path:            String,
    sniff_timestamp: String,
    last_modified:   String,
    delimiter_char:  char,
    header_row:      bool,
    preamble_rows:   usize,
    quote_char:      String,
    flexible:        bool,
    is_utf8:         bool,
    detected_mime:   String,
    detected_label:  String,
    inference_score: Option<f32>,
    retrieved_size:  usize,
    file_size:       usize,
    sampled_records: usize,
    estimated:       bool,
    num_records:     usize,
    avg_record_len:  usize,
    num_fields:      usize,
    stats_types:     bool,
    fields:          Vec<String>,
    types:           Vec<String>,
}
impl fmt::Display for SniffStruct {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "Path: {}",
            // when sniffing a snappy compressed file, it is first decompressed
            // to a temporary file. The original file name is stored in the
            // temporary file name, so we extract the original file name
            if self.path.contains("qsv_temp_decompressed__") {
                // use a regular expression to extract the original file name
                // the original file name after "qsv_temp_decompressed__"
                let re = regex::Regex::new(r"qsv_temp_decompressed__(?P<filename>.*)$").unwrap();
                let caps = re.captures(&self.path).unwrap();
                let filename = caps.name("filename").unwrap().as_str();
                filename.to_string()
            } else {
                self.path.clone()
            }
        )?;
        writeln!(f, "Sniff Timestamp: {}", self.sniff_timestamp)?;
        writeln!(f, "Last Modified: {}", self.last_modified)?;
        writeln!(
            f,
            "Delimiter: {}",
            if self.delimiter_char == '\t' {
                "tab".to_string()
            } else {
                self.delimiter_char.to_string()
            }
        )?;
        writeln!(f, "Header Row: {}", self.header_row)?;
        writeln!(
            f,
            "Preamble Rows: {}",
            HumanCount(self.preamble_rows as u64)
        )?;
        writeln!(f, "Quote Char: {}", self.quote_char)?;
        writeln!(f, "Flexible: {}", self.flexible)?;
        writeln!(f, "Is UTF8: {}", self.is_utf8)?;
        writeln!(f, "Detected Mime Type: {}", self.detected_mime)?;
        writeln!(f, "Detected Label: {}", self.detected_label)?;
        if let Some(score) = self.inference_score {
            writeln!(f, "Inference Score: {}", util::round_num(score.into(), 4))?;
        }
        writeln!(
            f,
            "Retrieved Size (bytes): {}",
            HumanCount(self.retrieved_size as u64)
        )?;
        // show "Unknown" for usize::MAX (sentinel value when Content-Length is missing)
        if self.file_size == usize::MAX {
            writeln!(f, "File Size (bytes): Unknown")?;
        } else {
            writeln!(
                f,
                "File Size (bytes): {}",
                HumanCount(self.file_size as u64)
            )?;
        }
        writeln!(
            f,
            "Sampled Records: {}",
            HumanCount(self.sampled_records as u64)
        )?;
        writeln!(f, "Estimated: {}", self.estimated)?;
        // show "Unknown" when num_records was estimated from unknown file_size
        if self.file_size == usize::MAX {
            writeln!(f, "Num Records: Unknown")?;
        } else {
            writeln!(f, "Num Records: {}", HumanCount(self.num_records as u64))?;
        }
        writeln!(
            f,
            "Avg Record Len (bytes): {}",
            HumanCount(self.avg_record_len as u64)
        )?;
        writeln!(f, "Num Fields: {}", HumanCount(self.num_fields as u64))?;
        writeln!(f, "Stats Types: {}", self.stats_types)?;
        writeln!(f, "Fields:")?;

        let mut tabwtr = TabWriter::new(vec![]);

        for (i, ty) in self.types.iter().enumerate() {
            let data_type = if self.stats_types {
                match ty.as_str() {
                    "Unsigned" | "Signed" => "Integer",
                    "Text" => "String",
                    _ => ty,
                }
            } else {
                ty
            };

            writeln!(
                &mut tabwtr,
                "\t{}:\t{data_type}\t{}",
                i + 1,
                self.fields.get(i).unwrap_or(&String::new())
            )
            .unwrap_or_default();
        }
        tabwtr.flush().unwrap();

        let tabbed_field_list = String::from_utf8(tabwtr.into_inner().unwrap()).unwrap();
        writeln!(f, "{tabbed_field_list}")?;

        Ok(())
    }
}

#[derive(Debug, Clone)]
struct SniffFileStruct {
    display_path:       String,
    file_to_sniff:      String,
    detected_mime:      String,
    detected_label:     String,
    inference_score:    Option<f32>,
    tempfile_flag:      bool,
    retrieved_size:     usize,
    file_size:          usize,
    last_modified:      String,
    downloaded_records: usize,
}

const fn rowcount(
    metadata: &csv_nose::metadata::Metadata,
    sniff_file_info: &SniffFileStruct,
    count: usize,
) -> (usize, bool) {
    let mut estimated = false;
    let rowcount = if count == usize::MAX {
        // if the count == usize::MAX, it's a sentinel value for "Unknown" as the server
        // didn't provide a Content-Length header, so we estimate the rowcount by
        // dividing the file_size by avg_rec_len
        estimated = true;
        sniff_file_info.file_size / metadata.avg_record_len
    } else {
        count
    };

    let has_header_row = metadata.dialect.header.has_header_row;
    let num_preamble_rows = metadata.dialect.header.num_preamble_rows;
    let mut final_rowcount = rowcount;

    if !has_header_row {
        final_rowcount += 1;
    }

    final_rowcount -= num_preamble_rows;
    (final_rowcount, estimated)
}

async fn get_file_to_sniff(args: &Args, tmpdir: &tempfile::TempDir) -> CliResult<SniffFileStruct> {
    if let Some(uri) = args.arg_input.clone() {
        match uri {
            // its a URL, download sample to temp file
            url if Url::parse(&url).is_ok() && url.starts_with("http") => {
                // Transform GitHub blob URLs to raw URLs for direct file access
                let url = util::transform_github_url(&url);
                let snappy_flag = url.to_lowercase().ends_with(".sz");

                // setup the reqwest client
                let client = util::create_reqwest_async_client(
                    args.flag_user_agent.clone(),
                    util::timeout_secs(args.flag_timeout)
                        .unwrap_or(30)
                        .min(u16::MAX as u64) as u16,
                    Some(url.clone()),
                )?;

                let res = client
                    .get(url.clone())
                    .timeout(Duration::from_secs(
                        util::timeout_secs(args.flag_timeout).unwrap_or(30),
                    ))
                    .send()
                    .await?;

                // Check Content-Type header to detect HTML responses early
                // This helps catch cases where URLs serve HTML pages instead of raw files
                if let Some(content_type) = res.headers().get("content-type")
                    && let Ok(ct_str) = content_type.to_str()
                    && ct_str.contains("text/html")
                {
                    log::warn!(
                        "Response Content-Type is text/html. URL may not point to raw file \
                         content."
                    );
                }

                let last_modified = match res.headers().get("Last-Modified") {
                    Some(lm) if let Ok(s) = lm.to_str() => {
                        // convert Last-Modified RFC2822 to RFC3339 format
                        let dt = chrono::DateTime::parse_from_rfc2822(s).unwrap();
                        dt.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)
                    },
                    _ => String::from("Unknown"),
                };

                let total_size = match res.content_length() {
                    Some(l) => l as usize,
                    None => {
                        // if we can't get the content length, just set it to a large value
                        // so we just end up downloading the entire file
                        usize::MAX
                    },
                };

                #[allow(clippy::cast_precision_loss)]
                let lines_sample_size = if snappy_flag {
                    // if it's a snappy compressed file, we need to download the entire file
                    // to uncompress and sniff it
                    usize::MAX
                } else if args.flag_sample > 1.0 {
                    args.flag_sample.round() as usize
                } else if args.flag_sample.abs() < f64::EPSILON {
                    // sample size is zero, so we want to download the entire file
                    usize::MAX
                } else {
                    // sample size is a percentage, download percentage number of lines
                    // from the file. Since we don't know how wide the lines are, we
                    // just download a percentage of the bytes, assuming the lines are
                    // 100 characters wide as a rough estimate.
                    ((total_size / 100_usize) as f64 * args.flag_sample) as usize
                };

                // prep progress bar
                #[cfg(any(feature = "feature_capable", feature = "lite"))]
                let show_progress =
                    args.flag_progressbar || util::get_envvar_flag("QSV_PROGRESSBAR");

                #[cfg(any(feature = "feature_capable", feature = "lite"))]
                let progress = ProgressBar::with_draw_target(
                    Some(total_size.try_into().unwrap_or(u64::MAX)),
                    ProgressDrawTarget::stderr_with_hz(5),
                );
                #[cfg(any(feature = "feature_capable", feature = "lite"))]
                if show_progress {
                    progress.set_style(
                        #[allow(clippy::to_string_in_format_args)]
                        #[allow(clippy::literal_string_with_formatting_args)]
                        ProgressStyle::default_bar()
                            .template(
                                "{msg}\n{spinner:.green} [{elapsed_precise}] \
                                 [{wide_bar:.white/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, \
                                 {eta})",
                            )
                            .unwrap(),
                    );
                    if lines_sample_size == usize::MAX {
                        progress.set_message(format!(
                            "Downloading {}...",
                            HumanBytes(total_size as u64)
                        ));
                    } else {
                        progress.set_message(format!(
                            "Downloading {} samples...",
                            HumanCount(lines_sample_size as u64)
                        ));
                    }
                } else {
                    progress.set_draw_target(ProgressDrawTarget::hidden());
                }

                let mut file = NamedTempFile::new()?;
                let mut downloaded = 0_usize;
                let mut stream = res.bytes_stream();
                #[cfg(any(feature = "feature_capable", feature = "lite"))]
                let mut downloaded_lines = 0_usize;
                #[allow(unused_assignments)]
                let mut chunk = Bytes::new(); // amortize the allocation
                let mut firstchunk = Bytes::new();

                // download chunks until we have the desired sample size
                while let Some(item) = stream.next().await {
                    chunk = item?;

                    file.write_all(&chunk)?;
                    let chunk_len = chunk.len();

                    if downloaded == 0 && !snappy_flag && args.flag_quick {
                        let (mime, _kind, _score) = detect_mime_from_bytes(&chunk[..]);
                        log::debug!("scanned first {chunk_len} bytes - detected mime: {mime}");
                        if !mime.starts_with("text/") && mime != "application/csv" {
                            downloaded = chunk_len;
                            firstchunk = chunk.clone();
                            break;
                        }
                    }

                    downloaded = min(downloaded + chunk_len, total_size);

                    #[cfg(any(feature = "feature_capable", feature = "lite"))]
                    if show_progress {
                        progress.inc(chunk_len as u64);
                    }

                    // check if we're downloading the entire file
                    #[cfg(any(feature = "feature_capable", feature = "lite"))]
                    if lines_sample_size != usize::MAX {
                        // we're not downloading the entire file, so we need to
                        // scan chunk for newlines
                        let num_lines = chunk.into_iter().filter(|&x| x == b'\n').count();
                        // and keep track of the number of lines downloaded which is ~= sample_size
                        downloaded_lines += num_lines;
                        // we downloaded enough samples, stop downloading
                        if downloaded_lines > lines_sample_size {
                            downloaded_lines -= 1; // subtract 1 because we don't want to count the header row
                            break;
                        }
                    }
                }
                drop(client);
                file.flush()?;

                #[cfg(any(feature = "feature_capable", feature = "lite"))]
                if show_progress {
                    if snappy_flag {
                        progress.finish_with_message(format!(
                            "Downloaded {}.",
                            HumanBytes(downloaded as u64)
                        ));
                    } else {
                        progress.finish_with_message(format!(
                            "Downloaded {} samples.",
                            HumanCount(downloaded_lines as u64)
                        ));
                    }
                }

                let wtr_file_path;
                let mut csv_candidate = true;
                let mut detected_mime = String::new();
                let mut detected_label: String = String::new();

                let mut detected_score: Option<f32> = None;
                if !args.flag_quick {
                    let (mime, label, score) = detect_mime_from_file(file.path())?;
                    detected_mime = mime;
                    detected_label = label;
                    detected_score = Some(score);
                    csv_candidate =
                        detected_mime.starts_with("text/") || detected_mime == "application/csv";
                }

                // create a temporary file to write the download file to
                let wtr_file = NamedTempFile::new()?;

                // keep the temporary file around so we can sniff it later
                // we'll delete it when we're done
                let (mut tmp_file, path) =
                    wtr_file.keep().map_err(|_| "Cannot keep temporary file")?;

                let mut downloaded_records = 0_usize;

                if snappy_flag {
                    // we downloaded a snappy compressed file, we need to decompress it
                    // before we can sniff it
                    wtr_file_path =
                        util::decompress_snappy_file(&file.path().to_path_buf(), tmpdir)?;
                } else if args.flag_quick && !csv_candidate {
                    // when --quick is enabled, we short-circuit downloading by checking
                    // the file type from the first chunk. If the file is not a CSV,
                    // we just write the first chunk to a file and return
                    wtr_file_path = path.display().to_string();
                    tmp_file.write_all(&firstchunk)?;
                    tmp_file.flush()?;
                } else if csv_candidate {
                    // we downloaded a non-snappy file and it might be a CSV file.
                    // Rewrite it so we only have the exact sample size and truncate potentially
                    // incomplete lines. We do this coz we streamed the download and the downloaded
                    // file may be more than the sample size, and the final line may be incomplete.
                    wtr_file_path = path.display().to_string();
                    let mut wtr = Config::new(Some(wtr_file_path.clone()).as_ref())
                        .no_headers(false)
                        .flexible(true)
                        .quote_style(csv::QuoteStyle::NonNumeric)
                        .writer()?;

                    let retrieved_name = file.path().to_str().unwrap().to_string();
                    let config = Config::new(Some(retrieved_name).as_ref())
                        .delimiter(args.flag_delimiter)
                        // we say no_headers so we can just copy the downloaded file over
                        // including headers, to the exact sample size file
                        .no_headers(true)
                        .flexible(true)
                        // skip format check since temp files don't have recognizable extensions
                        .skip_format_check(true);

                    let mut rdr = config.reader()?;

                    // amortize allocation
                    #[allow(unused_assignments)]
                    let mut record = csv::ByteRecord::with_capacity(100, 20);

                    let header_row = rdr.byte_headers()?;
                    wtr.write_byte_record(header_row)?;
                    rdr.byte_records().next();

                    for rec in rdr.byte_records() {
                        record = rec?;
                        if downloaded_records >= lines_sample_size {
                            break;
                        }
                        downloaded_records += 1;
                        wtr.write_byte_record(&record)?;
                    }
                    wtr.flush()?;
                } else {
                    // we downloaded a non-snappy file and its not a CSV file just copy it over
                    wtr_file_path = path.display().to_string();
                    file.seek(SeekFrom::Start(0))?;
                    copy(&mut file, &mut tmp_file)?;
                    log::debug!("Not a CSV file, just copy it over");
                }

                Ok(SniffFileStruct {
                    display_path: url,
                    file_to_sniff: wtr_file_path,
                    detected_mime,
                    detected_label,
                    inference_score: detected_score,
                    tempfile_flag: true,
                    retrieved_size: downloaded,
                    file_size: if total_size == usize::MAX {
                        // the server didn't give us content length
                        // so send usize::MAX to indicate that we don't know the file size
                        usize::MAX
                    } else {
                        total_size
                    },
                    last_modified,
                    downloaded_records,
                })
            },
            // its a file. If its a snappy file, decompress it first
            // aftwerwards, check if its one of the supported file types
            // finally, check if its a utf8 file
            path => {
                let mut path = path;

                let mut pathbuf = PathBuf::from(path.clone());
                let file_ext = pathbuf.extension();
                match file_ext {
                    Some(ext) => {
                        let mut lower_ext =
                            ext.to_str().unwrap().to_lowercase().as_str().to_owned();
                        if lower_ext == "sz" {
                            path = util::decompress_snappy_file(&pathbuf, tmpdir)?;
                            pathbuf = PathBuf::from(path.clone());
                            lower_ext = pathbuf
                                .extension()
                                .unwrap()
                                .to_os_string()
                                .into_string()
                                .unwrap();
                            log::info!("Decompressed {lower_ext} file to {path}");
                        }
                    },
                    None => {
                        log::warn!("File extension not found");
                    },
                }

                // resolve symlinks first so MIME detection and paths
                // (display_path/file_to_sniff) reflect the target file
                // safety: unwrap is safe as qsv requires UTF-8 paths throughout
                let canonical_path = fs::canonicalize(&path)?.to_str().unwrap().to_string();

                let metadata = fs::metadata(&canonical_path)?;

                let file_size = metadata.len() as usize;
                let last_modified = match metadata.modified() {
                    Ok(time) => {
                        // format the datetime to RFC3339
                        format_systemtime(time, "%+")
                    },
                    Err(_) => "N/A".to_string(),
                };

                Ok(SniffFileStruct {
                    display_path: canonical_path.clone(),
                    file_to_sniff: canonical_path,
                    detected_mime: String::new(),
                    detected_label: String::new(),
                    inference_score: None,
                    tempfile_flag: false,
                    retrieved_size: file_size,
                    file_size,
                    last_modified,
                    downloaded_records: 0,
                })
            },
        }
    } else {
        // read from stdin and write to a temp file
        let mut stdin_file = NamedTempFile::new()?;
        let stdin = std::io::stdin();
        let mut stdin_handle = stdin.lock();
        std::io::copy(&mut stdin_handle, &mut stdin_file)?;
        drop(stdin_handle);
        let (file, path) = stdin_file
            .keep()
            .map_err(|_| "Cannot keep temporary file")?;

        if !util::isutf8_file(&path)? {
            return fail_clierror!("stdin input is not UTF8-encoded");
        }

        let metadata = file.metadata()?;

        let file_size = metadata.len() as usize;
        // set last_modified to now in RFC3339 format
        let last_modified = chrono::Utc::now().format("%+").to_string();
        let path_string = path
            .into_os_string()
            .into_string()
            .unwrap_or_else(|_| "???".to_string());

        Ok(SniffFileStruct {
            display_path: "stdin".to_string(),
            file_to_sniff: path_string,
            detected_mime: String::new(),
            detected_label: String::new(),
            inference_score: None,
            tempfile_flag: true,
            retrieved_size: file_size,
            file_size,
            last_modified,
            downloaded_records: 0,
        })
    }
}

fn cleanup_tempfile(
    tempfile_flag: bool,
    tempfile: String,
) -> Result<(), crate::clitypes::CliError> {
    if tempfile_flag {
        fs::remove_file(tempfile)?;
    }
    Ok(())
}

pub fn run(argv: &[&str]) -> CliResult<()> {
    let args: Args = util::get_args(USAGE, argv)?;

    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(sniff_main(args))?;

    Ok(())
}

/// Minimum number of rows the contiguous sniffer samples, regardless of a smaller
/// `--sample` budget - too few rows make dialect/type detection unreliable.
const SNIFF_MIN_ROWS: usize = 20;

#[allow(clippy::unused_async)] // false positive lint
async fn sniff_main(mut args: Args) -> CliResult<()> {
    if args.flag_harvest_mode {
        args.flag_quick = true;
        args.flag_timeout = 10;
        args.flag_stats_types = true;
        args.flag_json = true;
        args.flag_user_agent =
            Some("CKAN-harvest/$QSV_VERSION ($QSV_TARGET; $QSV_BIN_NAME)".to_string());
    }

    if args.flag_just_mime {
        args.flag_no_infer = true;
    }

    let mut sample_size = args.flag_sample;
    let flag_json = args.flag_json || args.flag_pretty_json;
    if sample_size < 0.0 {
        if flag_json {
            let json_result = json!({
                "errors": [{
                    "title": "sniff error",
                    "detail": "Sample size must be greater than or equal to zero."
                }]
            });
            return fail_clierror!("{json_result}");
        }
        return fail_incorrectusage_clierror!("Sample size must be greater than or equal to zero.");
    }

    let sniffed_ts = chrono::Utc::now().to_rfc3339();
    let tmpdir = tempfile::tempdir()?;

    let future = get_file_to_sniff(&args, &tmpdir);
    let sfile_info = block_on(future)?;
    let tempfile_to_delete = sfile_info.file_to_sniff.clone();

    // if we don't have a mime type or its a snappy file and --no-infer is disabled,
    // let's try to infer the mime type
    let file_label: String;
    let file_score: Option<f32>;
    let file_type = if sfile_info.detected_mime.is_empty()
        || sfile_info.detected_mime == "application/x-snappy-framed" && !args.flag_no_infer
    {
        let (mime, label, score) =
            detect_mime_from_file(std::path::Path::new(&sfile_info.file_to_sniff))?;
        file_label = label;
        file_score = Some(score);
        mime
    } else {
        file_label = sfile_info.detected_label.clone();
        file_score = sfile_info.inference_score;
        sfile_info.detected_mime.clone()
    };

    // if the file is not a CSV candidate or --no-infer is specified,
    // we can just return the file type and exit
    if (file_type != "application/csv" && !file_type.starts_with("text/")) || args.flag_no_infer {
        cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;

        let size = if sfile_info.file_size == usize::MAX {
            "Unknown".to_string()
        } else {
            sfile_info.file_size.to_string()
        };
        let last_modified = sfile_info.last_modified;

        if flag_json {
            if args.flag_no_infer {
                let json_result = json!({
                    "title": "sniff mime type",
                    "meta": {
                        "detected_mime_type": file_type,
                        "detected_label": file_label,
                        "size": size,
                        "last_modified": last_modified,
                    }
                });
                if args.flag_pretty_json {
                    woutinfo!("{}", simd_json::to_string_pretty(&json_result).unwrap());
                    return Ok(());
                }
                woutinfo!("{json_result}");
                return Ok(());
            }
            let json_result = json!({
                "errors": [{
                    "title": "sniff error",
                    "detail": format!("File is not a CSV file. Detected mime type: {file_type}"),
                    "meta": {
                        "detected_mime_type": file_type,
                        "detected_label": file_label,
                        "size": size,
                        "last_modified": last_modified,
                    }
                }]
            });
            if args.flag_pretty_json {
                return fail_clierror!("{}", simd_json::to_string_pretty(&json_result).unwrap());
            }
            return fail_clierror!("{json_result}");
        }
        if args.flag_no_infer {
            woutinfo!(
                "Detected mime type: {file_type}, size: {size}, last modified: {last_modified}"
            );
            return Ok(());
        }
        return fail_clierror!(
            "File is not a CSV file. Detected mime type: {file_type}, size: {size}, last \
             modified: {last_modified}"
        );
    }

    let conf = Config::new(Some(sfile_info.file_to_sniff.clone()).as_ref())
        .flexible(true)
        .delimiter(args.flag_delimiter);
    let n_rows = if sfile_info.downloaded_records == 0 {
        //if we have the whole file and not just a sample, we can count the number of rows
        match util::count_rows_regular(&conf) {
            Ok(n) => n as usize,
            Err(e) => {
                cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;

                if flag_json {
                    let json_result = json!({
                        "errors": [{
                            "title": "count rows error",
                            "detail": e.to_string()
                        }]
                    });
                    return fail_clierror!("{json_result}");
                }
                return fail_clierror!("{}", e);
            },
        }
    } else {
        // usize::MAX is a sentinel value to let us
        // know that we need to estimate the number of records
        // since we only downloaded a sample, not the entire file
        usize::MAX
    };

    // its an empty file, exit with an error
    if n_rows == 0 {
        cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;

        if flag_json {
            let json_result = json!({
                "errors": [{
                    "title": "sniff error",
                    "detail": "Empty file"
                }]
            });
            return fail_clierror!("{json_result}");
        }
        return fail_clierror!("Empty file");
    }

    let mut sample_all = false;
    // its a percentage, get the actual sample size
    #[allow(clippy::cast_precision_loss)]
    if sample_size < 1.0 {
        sample_size *= n_rows as f64;
    } else if (sample_size).abs() < f64::EPSILON {
        // its zero, the epsilon bit is because comparing a float
        // is really not precise - see https://floating-point-gui.de/errors/comparison/
        sample_all = true;
    }

    // for a local file and stdin, set sampled_records to the sample size
    // for a remote file, set sampled_records to the number of rows downloaded
    let sampled_records = if sfile_info.downloaded_records == 0 {
        if sample_all {
            sample_size as usize
        } else {
            // the contiguous sniffer floors the sample at SNIFF_MIN_ROWS (below),
            // so report the number actually sniffed, not a smaller requested budget
            (sample_size as usize).max(SNIFF_MIN_ROWS)
        }
    } else {
        sample_all = true;
        sfile_info.downloaded_records
    };

    let rdr = conf.clone().skip_format_check(true).reader_file()?;

    let dt_preference = if args.flag_prefer_dmy || conf.get_dmy_preference() {
        DatePreference::DmyFormat
    } else {
        DatePreference::MdyFormat
    };

    if let Some(save_urlsample) = args.flag_save_urlsample {
        fs::copy(sfile_info.file_to_sniff.clone(), save_urlsample)?;
    }

    let quote_char = match args.flag_quote {
        Some(quote_char) => {
            csv_nose::metadata::Quote::Some(*quote_char.to_string().as_bytes().first().unwrap())
        },
        _ => csv_nose::metadata::Quote::None,
    };

    // now that we have all the sniffing parameters, we can sniff the file
    let sniff_results = if sample_all {
        log::info!("Sniffing ALL rows...");
        if let Some(delimiter) = args.flag_delimiter {
            Sniffer::new()
                .sample_size(SampleSize::All)
                .date_preference(dt_preference)
                .delimiter(delimiter.as_byte())
                .quote(quote_char)
                .sniff_reader(rdr.into_inner())
        } else {
            Sniffer::new()
                .sample_size(SampleSize::All)
                .date_preference(dt_preference)
                .sniff_reader(rdr.into_inner())
        }
    } else {
        let sniff_size = (sample_size as usize).max(SNIFF_MIN_ROWS);
        log::info!("Sniffing {sniff_size} rows...");
        if let Some(delimiter) = args.flag_delimiter {
            Sniffer::new()
                .sample_size(SampleSize::Records(sniff_size))
                .date_preference(dt_preference)
                .delimiter(delimiter.as_byte())
                .quote(quote_char)
                .sniff_reader(rdr.into_inner())
        } else {
            Sniffer::new()
                .sample_size(SampleSize::Records(sniff_size))
                .date_preference(dt_preference)
                .sniff_reader(rdr.into_inner())
        }
    };

    let mut processed_results = SniffStruct::default();
    let mut sniff_error: Option<String> = None;

    match sniff_results {
        Ok(metadata) => {
            let (num_records, estimated) = rowcount(&metadata, &sfile_info, n_rows);

            let sniffedfields = metadata
                .fields
                .iter()
                .map(std::string::ToString::to_string)
                .collect();

            // For a local file with a fresh CSV index, infer types from a
            // distributed sample (first/last/quartile windows + random rows up to
            // the --sample budget) instead of just the first N rows, catching
            // types/dates that only appear late in the file. Falls back to the
            // contiguous PASS 1 types when not indexed or on any issue.
            let mut effective_sampled_records = sampled_records;
            let types_source = if sfile_info.downloaded_records == 0
                && !sample_all
                && let Some((dist_types, dist_count)) = distributed_types(
                    &conf,
                    &metadata,
                    sample_size as usize,
                    dt_preference,
                    &tmpdir,
                )? {
                effective_sampled_records = dist_count;
                dist_types
            } else {
                metadata.types.clone()
            };
            let sniffedtypes: Vec<String> = types_source
                .iter()
                .map(std::string::ToString::to_string)
                .collect();

            let delimiter_char = metadata.dialect.delimiter as char;
            processed_results = SniffStruct {
                path: sfile_info.display_path,
                sniff_timestamp: sniffed_ts,
                last_modified: sfile_info.last_modified.clone(),
                delimiter_char,
                header_row: metadata.dialect.header.has_header_row,
                preamble_rows: metadata.dialect.header.num_preamble_rows,
                quote_char: match metadata.dialect.quote {
                    csv_nose::metadata::Quote::Some(chr) => format!("{}", char::from(chr)),
                    csv_nose::metadata::Quote::None => "none".into(),
                },
                flexible: metadata.dialect.flexible,
                is_utf8: metadata.dialect.is_utf8,
                detected_mime: if delimiter_char == ',' {
                    "application/csv".to_string()
                } else {
                    file_type.clone()
                },
                detected_label: if delimiter_char == ',' {
                    "csv".to_string()
                } else if delimiter_char == '\t' {
                    "tsv".to_string()
                } else {
                    file_label.clone()
                },
                inference_score: file_score.map(|score| {
                    // round to 4 decimal places without string round-trip
                    (score * 10_000.0).round() / 10_000.0
                }),
                retrieved_size: sfile_info.retrieved_size,
                file_size: sfile_info.file_size,
                sampled_records: if effective_sampled_records > num_records {
                    num_records
                } else {
                    effective_sampled_records
                },
                estimated,
                num_records,
                avg_record_len: metadata.avg_record_len,
                num_fields: metadata.num_fields,
                fields: sniffedfields,
                types: sniffedtypes,
                stats_types: args.flag_stats_types,
            };
        },
        Err(e) => {
            sniff_error = Some(format!("{e}"));
        },
    }

    cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;

    // safety: we just created all these json values above, so they are safe to unwrap
    if flag_json {
        if sniff_error.is_none() {
            if args.flag_pretty_json {
                println!(
                    "{}",
                    simd_json::to_string_pretty(&processed_results).unwrap()
                );
            } else {
                println!("{}", simd_json::to_string(&processed_results).unwrap());
            }
            return Ok(());
        }
        let sniff_error_json = json!({
            "title": "sniff error",
            // safety: we just created this string above, so it's safe to unwrap
            "detail": &sniff_error.unwrap(),
            "meta": {
                "detected_mime_type": file_type,
                "detected_label": file_label,
                "size": sfile_info.file_size,
                "last_modified": sfile_info.last_modified,
            }
        });
        let error_msg = if args.flag_pretty_json {
            simd_json::to_string_pretty(&sniff_error_json).unwrap()
        } else {
            simd_json::to_string(&sniff_error_json).unwrap()
        };
        return fail_clierror!("{error_msg}");
    }

    if let Some(error) = sniff_error {
        fail_clierror!(
            "{error}; size: {size}, last_modified: {last_modified}",
            size = sfile_info.file_size,
            last_modified = sfile_info.last_modified
        )
    } else {
        println!("{processed_results}");
        Ok(())
    }
}

fn distributed_types(
    conf: &Config,
    dialect_meta: &csv_nose::Metadata,
    budget: usize,
    dt_preference: DatePreference,
    tmpdir: &tempfile::TempDir,
) -> CliResult<Option<(Vec<csv_nose::Type>, usize)>> {
    // The index addresses raw file records and cannot account for a preamble/header
    // offset, so for files with preamble rows we fall back to the contiguous sample.
    if dialect_meta.dialect.header.num_preamble_rows > 0 {
        return Ok(None);
    }

    // Run the fallible distributed-sampling path inside a closure. Any I/O error
    // (opening the index, seeking, reading records, writing/reopening the temp file)
    // degrades to the contiguous PASS 1 types rather than failing the whole sniff -
    // mirroring util::count_rows_regular, which ignores unusable indexes.
    let attempt = || -> CliResult<Option<(Vec<csv_nose::Type>, usize)>> {
        // FIXED_ROWS is the max number of fixed positional rows we always include
        // (first 5 + last 5 + 5 each around Q1/Q2/Q3).
        const FIXED_ROWS: u64 = 25;

        let has_header = dialect_meta.dialect.header.has_header_row;

        // Build an index handle whose header setting matches the detected dialect so
        // count()/seek() row indices line up with our 0-based data-row math, and
        // whose delimiter & quote are the ones PASS 1 detected so records are split
        // (and round-tripped to the PASS 2 temp file) consistently with how PASS 2
        // is sniffed below - including for non-comma/odd-quote dialects (TSV, SSV,
        // backtick-quoted, unquoted, etc.).
        let mut idx_conf = conf
            .clone()
            .no_headers(!has_header)
            .delimiter(Some(Delimiter(dialect_meta.dialect.delimiter)));
        idx_conf = match dialect_meta.dialect.quote {
            csv_nose::Quote::Some(q) => idx_conf.quote(q),
            csv_nose::Quote::None => idx_conf.quoting(false),
        };
        let Some(mut idx) = idx_conf.indexed()? else {
            return Ok(None);
        };

        let total = idx.count();
        // Fall back to the contiguous sample when distributed sampling buys nothing
        // or would violate the --sample budget:
        //  - total <= budget: the contiguous sample already reads ~everything;
        //  - total <= FIXED_ROWS: too small to distribute meaningfully;
        //  - budget < FIXED_ROWS: the fixed windows alone (~25 rows) would exceed the requested
        //    budget, over-reporting sampled_records.
        if total <= budget as u64 || total <= FIXED_ROWS || (budget as u64) < FIXED_ROWS {
            return Ok(None);
        }
        let last = total - 1;

        // --- select distributed row indices (0-based over data rows) ---
        // BTreeSet keeps indices sorted (so seeks move forward, minimizing I/O) and
        // dedups overlapping windows automatically.
        let mut selected: BTreeSet<u64> = BTreeSet::new();

        // first 5 and last 5
        for i in 0..5 {
            selected.insert(i);
        }
        for i in total - 5..total {
            selected.insert(i);
        }
        // +-2 windows around Q1, Q2 (median) and Q3
        for anchor in [total / 4, total / 2, 3 * total / 4] {
            let lo = anchor.saturating_sub(2);
            let hi = (anchor + 2).min(last);
            for i in lo..=hi {
                selected.insert(i);
            }
        }

        // fill the remaining budget with random rows (total > budget guaranteed
        // above, so rejection sampling always terminates)
        let random_needed = budget.saturating_sub(selected.len());
        if random_needed > 0 {
            let mut rng: StdRng = rand::make_rng();
            let target = selected.len() + random_needed;
            while selected.len() < target {
                selected.insert(rng.random_range(0..total));
            }
        }
        let sampled_count = selected.len();

        // --- PASS 2: write the distributed rows to a temp CSV, then sniff with the
        // PASS 1 dialect forced. The temp file lives in `tmpdir`, so it is removed
        // when the caller's TempDir drops. The writer uses the detected delimiter &
        // quote so the round-trip is faithful to what PASS 2 expects. ---
        let dist_path = tmpdir.path().join("qsv_sniff_distributed.csv");
        let dist_path_str = dist_path.to_string_lossy().into_owned();

        let mut wtr_conf = Config::new(Some(dist_path_str.clone()).as_ref())
            .no_headers(true)
            .flexible(true)
            .delimiter(Some(Delimiter(dialect_meta.dialect.delimiter)));
        wtr_conf = match dialect_meta.dialect.quote {
            csv_nose::Quote::Some(q) => wtr_conf.quote(q),
            csv_nose::Quote::None => wtr_conf.quote_style(csv::QuoteStyle::Never),
        };
        let mut wtr = wtr_conf.writer()?;

        if has_header {
            // clone the header out before the seek/read loop, as both borrow idx
            // mutably
            let header = idx.byte_headers()?.clone();
            wtr.write_byte_record(&header)?;
        }

        for &i in &selected {
            idx.seek(i)?;
            let Some(rec) = idx.byte_records().next() else {
                // the index claimed a record we cannot read; degrade gracefully
                return Ok(None);
            };
            wtr.write_byte_record(&rec?)?;
        }
        wtr.flush()?;
        drop(wtr);

        let dist_rdr = Config::new(Some(dist_path_str).as_ref())
            .skip_format_check(true)
            .flexible(true)
            .reader_file()?;

        let pass2 = Sniffer::new()
            .sample_size(SampleSize::All)
            .date_preference(dt_preference)
            .delimiter(dialect_meta.dialect.delimiter)
            .quote(dialect_meta.dialect.quote)
            .sniff_reader(dist_rdr.into_inner());

        match pass2 {
            // only adopt PASS 2 types when it agrees with PASS 1 on the field count;
            // a mismatch means PASS 2 misparsed, so keep the authoritative PASS 1
            // types (which the caller zips against PASS 1 fields).
            Ok(meta) if meta.types.len() == dialect_meta.num_fields => {
                Ok(Some((meta.types, sampled_count)))
            },
            Ok(_) => Ok(None),
            Err(e) => {
                log::warn!("distributed-sample type inference failed: {e}");
                Ok(None)
            },
        }
    };

    match attempt() {
        Ok(result) => Ok(result),
        Err(e) => {
            log::warn!("distributed sampling failed ({e}); using contiguous sample");
            Ok(None)
        },
    }
}

/// Sniffs a local file in-process and returns the names of columns detected as
/// `Date` or `DateTime`, in field order.
///
/// Used by `stats --infer-dates` to resolve the "sniff" dates-whitelist WITHOUT
/// forking a `qsv sniff` subprocess (which would reload the qsv binary, re-sample
/// the input, and JSON round-trip the result). It reuses `get_file_to_sniff`
/// (so snappy decompression, symlink/canonicalization and delimiter handling stay
/// identical to the command) and mirrors the `qsv sniff --stats-types <path>`
/// defaults the subprocess used: sample the first 1000 rows (the `--sample`
/// default), auto-detect the delimiter, and honor the file's dmy/mdy preference.
pub(crate) fn date_columns(input_path: &std::path::Path) -> CliResult<Vec<String>> {
    let args = Args {
        arg_input:           Some(input_path.to_string_lossy().into_owned()),
        flag_sample:         1000.0,
        flag_prefer_dmy:     false,
        flag_json:           true,
        flag_save_urlsample: None,
        flag_pretty_json:    false,
        flag_delimiter:      None,
        flag_quote:          None,
        flag_progressbar:    false,
        flag_timeout:        30,
        flag_user_agent:     None,
        flag_stats_types:    true,
        flag_no_infer:       false,
        flag_just_mime:      false,
        flag_quick:          false,
        flag_harvest_mode:   false,
    };

    // get_file_to_sniff is async only for the remote-URL branch; for a local path
    // it resolves on the first poll, so block_on drives it without a tokio runtime.
    let tmpdir = tempfile::tempdir()?;
    let sfile_info = block_on(get_file_to_sniff(&args, &tmpdir))?;
    let tempfile_to_delete = sfile_info.file_to_sniff.clone();

    let conf = Config::new(Some(sfile_info.file_to_sniff.clone()).as_ref())
        .flexible(true)
        .delimiter(args.flag_delimiter);
    let rdr = conf.clone().skip_format_check(true).reader_file()?;

    let dt_preference = if conf.get_dmy_preference() {
        DatePreference::DmyFormat
    } else {
        DatePreference::MdyFormat
    };

    let sniff_results = Sniffer::new()
        .sample_size(SampleSize::Records(1000))
        .date_preference(dt_preference)
        .sniff_reader(rdr.into_inner());

    let date_cols = match sniff_results {
        Ok(metadata) => {
            // When the input is indexed, infer types from a distributed sample
            // (first/last/quartile windows + random) so date columns whose values
            // only appear late in the file are still detected. Falls back to the
            // contiguous PASS 1 types when not indexed or on any issue.
            let types = match distributed_types(&conf, &metadata, 1000, dt_preference, &tmpdir) {
                Ok(Some((dist_types, _))) => dist_types,
                _ => metadata.types.clone(),
            };
            metadata
                .fields
                .iter()
                .zip(types.iter())
                .filter_map(|(field, typ)| {
                    if matches!(*typ, csv_nose::Type::Date | csv_nose::Type::DateTime) {
                        Some(field.to_string())
                    } else {
                        None
                    }
                })
                .collect()
        },
        Err(e) => {
            cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;
            return fail_clierror!("Failed to sniff file for date columns: {e}");
        },
    };

    cleanup_tempfile(sfile_info.tempfile_flag, tempfile_to_delete)?;

    Ok(date_cols)
}
