use crate::core::{DosageMatrix, VariantInfo};

/// A single chunk from a streaming reader.
///
/// `total_variants` may be 0, meaning "unknown" (e.g. when streaming
/// compressed VCF without a pre-count pass).  The final chunk in a
/// stream will always have the correct total.
#[derive(Debug, Clone)]
pub struct GenotypeChunk {
    pub variants: VariantInfo,
    pub dosages: DosageMatrix,
    pub offset: usize,
    pub total_variants: usize,
}

impl GenotypeChunk {
    pub fn n_samples(&self) -> usize {
        self.dosages.n_samples()
    }

    pub fn n_variants(&self) -> usize {
        self.dosages.n_variants()
    }

    /// Returns `true` if this is the last chunk in the stream.
    ///
    /// When `total_variants` is unknown (0), returns `false` for all
    /// chunks except the final one (which will have the correct total
    /// stamped by the iterator).
    pub fn is_last(&self) -> bool {
        if self.total_variants == 0 {
            return false;
        }
        self.offset + self.n_variants() >= self.total_variants
    }

    /// Fraction of variants read so far (0.0–1.0).
    ///
    /// Returns -1.0 when the total is unknown (caller can use this
    /// to show an indeterminate progress indicator).
    pub fn progress(&self) -> f64 {
        if self.total_variants == 0 {
            return -1.0;
        }
        (self.offset + self.n_variants()) as f64 / self.total_variants as f64
    }
}
