use crate::{
    datafile::datafile::DataFile,
    info_log,
    targeting::{conditions::TargetingCondition, models::UnknownCPModel, TargetingData},
    warn_log,
};

pub(crate) struct UnknownCondition {
    pub(crate) targeting_type: String,
}

impl UnknownCondition {
    pub fn from_json(model: UnknownCPModel) -> Option<Self> {
        let targeting_type = model.targeting_type.unwrap_or("?".to_owned());
        info_log!("Unknown targeting condition type: '{}'", targeting_type);
        Some(Self { targeting_type })
    }
}

impl TargetingCondition for UnknownCondition {
    fn check(&self, _: &TargetingData<impl DataFile>) -> Option<bool> {
        warn_log!("Condition of unknown type: '{}' evaluated as true", self.targeting_type);
        Some(true)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        datafile::datafile_test_utils::MockDataFile, storage::visitor::Visitor, targeting::TargetingDataBuilder,
        utils::test_utils::assert_targeting,
    };

    use rstest::rstest;

    #[rstest]
    fn test_check() {
        let datafile = MockDataFile::default();
        let visitor = Visitor::new();
        assert_targeting(
            "resources/conditions/mock_segment_unknown.json",
            &TargetingDataBuilder::new().build(&datafile, &visitor.rdata()),
            true,
        );
    }
}
