use bls::{SecretKey, Signature};
use context_deserialize::context_deserialize;
use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use tree_hash_derive::TreeHash;

use crate::{
    core::{ChainSpec, EthSpec, Hash256, SignedRoot},
    fork::{Fork, ForkName},
    sync_committee::{SyncCommitteeContribution, SyncSelectionProof},
};

/// A Validators aggregate sync committee contribution and selection proof.
#[cfg_attr(
    feature = "arbitrary",
    derive(arbitrary::Arbitrary),
    arbitrary(bound = "E: EthSpec")
)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash)]
#[serde(bound = "E: EthSpec")]
#[context_deserialize(ForkName)]
pub struct ContributionAndProof<E: EthSpec> {
    /// The index of the validator that created the sync contribution.
    #[serde(with = "serde_utils::quoted_u64")]
    pub aggregator_index: u64,
    /// The aggregate contribution.
    pub contribution: SyncCommitteeContribution<E>,
    /// A proof provided by the validator that permits them to publish on the
    /// `sync_committee_contribution_and_proof` gossipsub topic.
    pub selection_proof: Signature,
}

impl<E: EthSpec> ContributionAndProof<E> {
    /// Produces a new `ContributionAndProof` with a `selection_proof` generated by signing
    /// `SyncAggregatorSelectionData` with `secret_key`.
    ///
    /// If `selection_proof.is_none()` it will be computed locally.
    pub fn from_aggregate(
        aggregator_index: u64,
        contribution: SyncCommitteeContribution<E>,
        selection_proof: Option<SyncSelectionProof>,
        secret_key: &SecretKey,
        fork: &Fork,
        genesis_validators_root: Hash256,
        spec: &ChainSpec,
    ) -> Self {
        let selection_proof = selection_proof
            .unwrap_or_else(|| {
                SyncSelectionProof::new::<E>(
                    contribution.slot,
                    contribution.subcommittee_index,
                    secret_key,
                    fork,
                    genesis_validators_root,
                    spec,
                )
            })
            .into();

        Self {
            aggregator_index,
            contribution,
            selection_proof,
        }
    }
}

impl<E: EthSpec> SignedRoot for ContributionAndProof<E> {}
