use crate::web::MyResponse;
use hagrid_database::{Database, Sqlite};
use rocket::get;

// WKD queries
#[get("/.well-known/openpgpkey/<domain>/hu/<wkd_hash>")]
pub fn wkd_query(db: &rocket::State<Sqlite>, domain: String, wkd_hash: String) -> MyResponse {
    match db.by_domain_and_hash_wkd(&domain, &wkd_hash) {
        Some(key) => MyResponse::wkd(key, &wkd_hash),
        None => MyResponse::not_found_plain("No key found for this email address."),
    }
}

// Policy requests.
// 200 response with an empty body.
#[get("/.well-known/openpgpkey/<_domain>/policy")]
pub fn wkd_policy(_domain: String) -> MyResponse {
    MyResponse::plain("".to_string())
}

#[cfg(test)]
mod tests {
    use crate::routes::tests::common::*;
    use rocket::http::{ContentType, Status};
    use rocket::local::blocking::Client;
    use rstest::rstest;
    use tempfile::TempDir;

    mod get_wkd_policy {
        use super::*;

        const URI: &str = "/.well-known/openpgpkey/example.org/policy";

        #[rstest]
        fn wkd_policy_respond_successfully_with_empty_body(
            #[from(client)] (_tmpdir, client): (TempDir, Client),
        ) {
            assert::response(
                client.get(URI).dispatch(),
                Status::Ok,
                ContentType::Plain,
                "",
            );
        }
    }
}
