// Copyright © 2023 Kim Altintop <kim@eagain.io>
// SPDX-License-Identifier: GPL-2.0-only

use std::collections::{
    HashMap,
    HashSet,
};

use async_trait::async_trait;
use yapma_common::http;

use super::{
    gpg,
    ssh,
};

pub struct Keys {
    pub ssh: HashSet<ssh::PublicKey>,
    pub gpg: HashMap<gpg::Fingerprint, gpg::SignedPublicKey>,
}

#[async_trait]
pub trait Resolver {
    type Error: Send + Sync;

    async fn resolve<C: http::Client>(&self, client: &C) -> Result<Keys, Self::Error> {
        tokio::try_join!(self.resolve_ssh(client), self.resolve_gpg(client))
            .map(|(ssh, gpg)| Keys { ssh, gpg })
    }

    async fn resolve_ssh<C: http::Client>(
        &self,
        client: &C,
    ) -> Result<HashSet<ssh::PublicKey>, Self::Error>;
    async fn resolve_gpg<C: http::Client>(
        &self,
        client: &C,
    ) -> Result<HashMap<gpg::Fingerprint, gpg::SignedPublicKey>, Self::Error>;
}