use jsonwebtoken::Header;
use serde::Deserialize;
use std::collections::HashSet;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IntrospectionResult {
    /// The token is a JWT Bearer token according to RFC 7523.
    JWTBearer {
        /// Token header
        header: Header,
        /// Issuer of the token
        iss: HashSet<String>,
        /// Audience of the token
        aud: HashSet<String>,
    },
    /// Unknown token format
    Unknown,
}

/// Introspect a token to determine its type and issuer.
///
/// **Warning**
/// This function does not validate the token, it only introspects it.
#[must_use]
pub fn introspect(token: &str) -> IntrospectionResult {
    let header = jsonwebtoken::decode_header(token);
    match header {
        Ok(header) => {
            let result: JWTBearer = match jsonwebtoken::dangerous::insecure_decode(token) {
                Ok(token_data) => token_data.claims,
                Err(e) => {
                    tracing::trace!(
                        "Token is not a JWT Bearer token. Could not decode claims: {e}"
                    );

                    return IntrospectionResult::Unknown;
                }
            };

            IntrospectionResult::JWTBearer {
                header,
                iss: result.iss.into_set(),
                aud: result.aud.into_set(),
            }
        }
        Err(e) => {
            tracing::trace!("Token is not a JWT Bearer token. Could not decode header: {e}");
            IntrospectionResult::Unknown
        }
    }
}

#[derive(Deserialize)]
pub(crate) struct JWTBearer {
    #[allow(unused)]
    sub: String,
    iss: Issuer,
    #[serde(default)]
    aud: Audience,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum Audience {
    Single(String),
    Multiple(HashSet<String>),
}

impl Default for Audience {
    fn default() -> Self {
        Audience::Multiple(HashSet::new())
    }
}

impl Audience {
    fn into_set(self) -> HashSet<String> {
        match self {
            Audience::Single(s) => HashSet::from([s]),
            Audience::Multiple(s) => s,
        }
    }
}

#[derive(Deserialize)]
#[serde(untagged)]
enum Issuer {
    Single(String),
    Multiple(HashSet<String>),
}

impl Issuer {
    fn into_set(self) -> HashSet<String> {
        match self {
            Issuer::Single(s) => HashSet::from([s]),
            Issuer::Multiple(s) => s,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tracing_test::traced_test;

    #[test]
    #[traced_test]
    fn test_introspect_jwt_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zaXZylgQSjszdbSx2UUx6-XKSNMa0o64TGNVkpRioj--JJ5ZSGtMVyioT_hMnT_hTUayStZNZ1Is80n3Pg11kh8qam6mZHvmqkTg4WXYkekGoOc1_SVDsI6QI084Ut4eBKPG_XtHP2ruTR_Y6WLbmQEFMkSPTB-TULHWZ8elwuGMWdAAV60oGQgvid4FHHwJyYXJLyb2NC3Q4XSb_7sS_cZIEWgO6hRUb9VYQq1tof0NT6WegUGbzhbSTfEOOEGJ3-3bquAoxskvOXTeVB7nzCw6e8KBnZS1PYtoiCR_9fp_Ag_7xukcgrfibn9k-BlN1w";
        let result = introspect(token);
        match result {
            IntrospectionResult::JWTBearer { header, iss, aud } => {
                assert_eq!(header.alg, jsonwebtoken::Algorithm::RS256);
                assert_eq!(iss.len(), 1);
                assert!(iss.contains("http://localhost:30080/realms/iceberg"));
                assert_eq!(aud.len(), 2);
                assert!(aud.contains("account"));
                assert!(aud.contains("lakekeeper"));
            }
            _ => panic!("Unexpected result: {result:?}"),
        }
    }

    #[test]
    #[traced_test]
    fn test_long_lived_kube_token() {
        let token = "eyJhbGciOiJSUzI1NiIsImtpZCI6Ill1aDZXRGtoUk9mcnUzb3lfekFSQXBBMklQYjdwaFdVN3F3Qkp4SURyOVEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Imxha2VrZWVwZXItc2EtdG9rZW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoibXktbGFrZWtlZXBlciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImI4ZTZlZTc1LTgzNDEtNGEzMC04YjNkLWU1YTIwZjRiOTFkYyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0Om15LWxha2VrZWVwZXIifQ.bwP_X8aBIkoDPyhmpyd1gBGIxreblgHZem1BHjhoyN3fSvMFdwg34muZAs7m3VlFphPQxQPdyvY6sqoKigCydbK1AS3-DdpdVG2jge2AKJlL27HEnWhDZwO8iD8orUlgPCNFd7qinK0FBEHOJKAAB3XSwGSt0nWL6cFcGoggbhE6IorbfPrpHHJMca7aTIu1Wo3QA4AHDekwqivWdO-CfRC7clVMjDogbd55qnxSMZnPkRQzJ7Loy9YRqzizoMo2yuaUEQ1Kfz-gDsMYBdhtzMLR25c-uVMSGNPombxImmza5YpNNbQNBA9JkQSydfGRVqGnCQcVhIZ4M8e9dc0Trw";
        let introspection_result = introspect(token);
        if let IntrospectionResult::JWTBearer { iss, .. } = introspection_result {
            assert_eq!(
                iss,
                HashSet::from(["kubernetes/serviceaccount".to_string()])
            );
        } else {
            panic!("Unexpected result: {introspection_result:?}");
        }
    }
}
