use std::fmt::Debug;

use crate::network::dto::VariationByExpositionDto;

#[cfg_attr(test, mockall::automock)]
pub(crate) trait VariationByExposition: Send + Sync + Debug {
    fn variation_key(&self) -> &str;
    fn variation_id(&self) -> u32;
    fn exposition(&self) -> f64;
}

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct VariationByExpositionImpl {
    variation_key: String,
    variation_id: u32,
    exposition: f64,
}

impl VariationByExposition for VariationByExpositionImpl {
    fn variation_key(&self) -> &str {
        &self.variation_key
    }

    fn variation_id(&self) -> u32 {
        self.variation_id
    }

    fn exposition(&self) -> f64 {
        self.exposition
    }
}

impl From<VariationByExpositionDto> for VariationByExpositionImpl {
    fn from(dto: VariationByExpositionDto) -> Self {
        Self::new(dto)
    }
}

impl VariationByExpositionImpl {
    pub fn new(dto: VariationByExpositionDto) -> Self {
        Self {
            variation_key: dto.variation_key,
            variation_id: dto.variation_id,
            exposition: dto.exposition,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::datafile::datafile_test_utils::make_variation_exposition_dto;
    use rstest::rstest;

    #[rstest]
    #[case("A", 123, 0.75)]
    #[case("B", 0, 0.5)]
    fn test_variation_exposition_impl_new(#[case] key: &str, #[case] variation_id: u32, #[case] exposition: f64) {
        // Arrange
        let dto = make_variation_exposition_dto(key, variation_id, exposition);
        let exposition_impl = VariationByExpositionImpl::new(dto);

        // Assert
        assert_eq!(exposition_impl.variation_key(), key);
        assert_eq!(exposition_impl.variation_id(), variation_id);
        assert_eq!(exposition_impl.exposition(), exposition);
    }

    #[rstest]
    #[case("A", 123, 0.75)]
    #[case("B", 0, 0.5)]
    fn test_variation_exposition_impl_from(#[case] key: &str, #[case] variation_id: u32, #[case] exposition: f64) {
        // Arrange
        let dto = make_variation_exposition_dto(key, variation_id, exposition);
        let exposition_impl: VariationByExpositionImpl = dto.into();

        // Assert
        assert_eq!(exposition_impl.variation_key(), key);
        assert_eq!(exposition_impl.variation_id(), variation_id);
        assert_eq!(exposition_impl.exposition(), exposition);
    }
}
