use radicle::node::{AliasStore, NodeId};

use crate::config::Config;

pub mod cobs;
pub mod issue;
pub mod job;
pub mod patch;
pub mod repo;
pub mod thread;

pub trait Profile {
    fn profile(&self) -> radicle::Profile;

    fn config(&self) -> Config {
        let p = self.profile();

        Config {
            public_key: p.public_key,
            alias: p.config.node.alias.clone(),
            seeding_policy: p.config.node.seeding_policy,
            public_explorer: p.config.public_explorer.clone(),
            preferred_seeds: p.config.preferred_seeds.clone(),
        }
    }

    fn alias(&self, nid: NodeId) -> Option<radicle::node::Alias> {
        let p = self.profile();
        let aliases = p.aliases();

        aliases.alias(&nid)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
    use std::str::FromStr;

    use radicle::crypto::{Seed, Signer, SigningKey};
    use radicle::node::{Alias, config};

    use crate::config::Config;
    use crate::{AppState, Profile, test};

    #[test]
    fn config() {
        let tmp = tempfile::tempdir().unwrap();
        let profile = test::profile(tmp.path(), [0xff; 32]);
        let signer = SigningKey::from_seed(Seed::new([0xff; 32]));
        let state = AppState { profile };

        assert_eq!(
            Profile::config(&state),
            Config {
                public_key: *signer.public_key(),
                alias: Alias::from_str("seed").unwrap(),
                seeding_policy: config::DefaultSeedingPolicy::Block,
                public_explorer: state.profile.config.public_explorer.clone(),
                preferred_seeds: state.profile.config.preferred_seeds.clone(),
            }
        )
    }
}
