use crate::models::{CertificateInfo, TlsReport, TlsStatus};

pub fn report_for_http() -> TlsReport {
    TlsReport {
        enabled: false,
        status: TlsStatus::NotUsed,
        version: None,
        cipher_suite: None,
        certificate: None,
        error_message: None,
    }
}

pub fn report_for_https_success(
    version: Option<String>,
    cipher_suite: Option<String>,
    certificate: Option<CertificateInfo>,
) -> TlsReport {
    TlsReport {
        enabled: true,
        status: TlsStatus::HandshakeSucceeded,
        version,
        cipher_suite,
        certificate,
        error_message: None,
    }
}

pub fn report_for_https_failure(error_message: impl Into<String>) -> TlsReport {
    TlsReport {
        enabled: true,
        status: TlsStatus::HandshakeFailed,
        version: None,
        cipher_suite: None,
        certificate: None,
        error_message: Some(error_message.into()),
    }
}

pub fn report_for_scheme(scheme: &str) -> TlsReport {
    match scheme {
        "https" => report_for_https_success(None, None, None),
        _ => report_for_http(),
    }
}

#[cfg(test)]
mod tests {
    use crate::models::TlsStatus;

    use super::{report_for_http, report_for_https_failure, report_for_https_success};

    #[test]
    fn report_for_http_marks_tls_as_unused() {
        let report = report_for_http();

        assert!(!report.enabled);
        assert_eq!(report.status, TlsStatus::NotUsed);
        assert_eq!(report.error_message, None);
    }

    #[test]
    fn report_for_https_success_marks_handshake_success() {
        let report = report_for_https_success(None, None, None);

        assert!(report.enabled);
        assert_eq!(report.status, TlsStatus::HandshakeSucceeded);
    }

    #[test]
    fn report_for_https_failure_carries_error_message() {
        let report = report_for_https_failure("certificate expired");

        assert!(report.enabled);
        assert_eq!(report.status, TlsStatus::HandshakeFailed);
        assert_eq!(report.error_message.as_deref(), Some("certificate expired"));
    }
}
