use crate::network::dto::ConfigurationDto;

pub const REQUIRED_CONSENT_TYPE: &str = "REQUIRED";

#[derive(Debug, Clone, PartialEq)]
enum ConsentType {
    Required,
    NotRequired,
}

impl From<&String> for ConsentType {
    fn from(s: &String) -> Self {
        match s.to_ascii_uppercase().as_str() {
            REQUIRED_CONSENT_TYPE => ConsentType::Required,
            _ => ConsentType::NotRequired,
        }
    }
}

#[cfg_attr(test, mockall::automock)]
pub(crate) trait Settings {
    fn consent_required(&self) -> bool;
    fn is_streaming_mode(&self) -> bool;
    fn data_api_domain(&self) -> &str;
    fn streaming_domain(&self) -> &str;
}

#[derive(Debug)]
pub(crate) struct SettingsImpl {
    configuration_dto: ConfigurationDto,
    consent_type: ConsentType,
}

impl Settings for SettingsImpl {
    fn data_api_domain(&self) -> &str {
        &self.configuration_dto.data_api_domain
    }

    fn streaming_domain(&self) -> &str {
        &self.configuration_dto.streaming_api_domain
    }

    fn consent_required(&self) -> bool {
        self.consent_type == ConsentType::Required
    }

    fn is_streaming_mode(&self) -> bool {
        self.configuration_dto.real_time_update
    }
}

impl SettingsImpl {
    pub fn new(configuration_dto: ConfigurationDto) -> Self {
        Self {
            consent_type: (&configuration_dto.consent_type).into(),
            configuration_dto,
        }
    }
}

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

    #[rstest]
    #[case("REQUIRED", true)]
    #[case("required", true)]
    #[case("NotRequired", false)]
    #[case("", false)]
    fn test_consent_required(#[case] consent_str: &str, #[case] expected: bool) {
        let dto = ConfigurationDto {
            data_api_domain: "data.example.com".to_string(),
            streaming_api_domain: "stream.example.com".to_string(),
            consent_type: consent_str.to_owned(),
            real_time_update: false,
        };
        let settings = SettingsImpl::new(dto);
        assert_eq!(settings.consent_required(), expected);
    }

    #[rstest]
    #[case(true)]
    #[case(false)]
    fn test_is_streaming_mode(#[case] real_time: bool) {
        let dto = make_configuration_dto(true, real_time, "data.example.com", "stream.example.com");
        let settings = SettingsImpl::new(dto);
        assert_eq!(settings.is_streaming_mode(), real_time);
    }

    #[rstest]
    #[case("data.example.com", "stream.example.com")]
    #[case("api.localhost", "stream.localhost")]
    fn test_domains(#[case] data_api: &str, #[case] streaming_api: &str) {
        let dto = make_configuration_dto(true, true, data_api, streaming_api);
        let settings = SettingsImpl::new(dto);
        assert_eq!(settings.data_api_domain(), data_api);
        assert_eq!(settings.streaming_domain(), streaming_api);
    }
}
