use crate::prelude::*;
use solana_program::pubkey::Pubkey;
use anchor_lang::ToAccountMetas;
use anchor_lang::{AnchorDeserialize, AnchorSerialize};

pub struct OracleQuoteRotate {}

#[derive(Clone, AnchorSerialize, AnchorDeserialize, Debug)]
pub struct OracleQuoteRotateParams {
    pub registry_key: [u8; 64],
    pub secp_pubkey: Option<[u8; 64]>,
    pub last_rotate_timestamp: u64,
}

impl InstructionData for OracleQuoteRotateParams {}

impl Discriminator for OracleQuoteRotateParams {
    const DISCRIMINATOR: [u8; 8] = [158, 59, 84, 20, 118, 112, 231, 61];
}

impl Discriminator for OracleQuoteRotate {
    const DISCRIMINATOR: [u8; 8] = [158, 59, 84, 20, 118, 112, 231, 61];
}

pub struct OracleQuoteRotateAccounts {
    pub oracle: Pubkey,
    pub authority: Pubkey,
    pub enclave_signer: Pubkey,
    pub queue: Pubkey,
}
impl ToAccountMetas for OracleQuoteRotateAccounts {
    fn to_account_metas(&self, _: Option<bool>) -> Vec<AccountMeta> {
        vec![
            AccountMeta::new(self.oracle, false),
            AccountMeta::new_readonly(self.authority, true),
            AccountMeta::new_readonly(self.enclave_signer, false),
            AccountMeta::new(self.queue, false),
        ]
    }
}

impl OracleQuoteRotate {
    pub fn build_ix(
        oracle: &Pubkey,
        oracle_authority: &Pubkey,
        enclave_signer: &Pubkey,
        queue: &Pubkey,
        registry_key: [u8; 64],
        last_rotate_timestamp: u64,
        secp_enclave_signer: Option<[u8; 64]>,
    ) -> Result<Instruction, OnDemandError> {
        Ok(crate::utils::build_ix(
            &*SWITCHBOARD_ATTESTATION_PROGRAM_ID,
            &OracleQuoteRotateAccounts {
                oracle: *oracle,
                authority: *oracle_authority,
                enclave_signer: *enclave_signer,
                queue: *queue,
            },
            &OracleQuoteRotateParams {
                registry_key,
                secp_pubkey: secp_enclave_signer,
                last_rotate_timestamp,
            },
        ))
    }
}
