use anchor_lang::prelude::*;

declare_id!("CaihzPH42a4J48sc3nQ9votPk4Xj8hQourX38otBAxp7");

#[program]
pub mod trust_data_bridge {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        let deployment_authorized = false;
        require!(deployment_authorized, BridgeError::BootstrapDisabled);
        let state = &mut ctx.accounts.bridge_state;
        state.authority = ctx.accounts.authority.key();
        state.total_messages = 0;
        state.created_at = Clock::get()?.unix_timestamp;
        Ok(())
    }

    pub fn submit_bridge_message(
        _ctx: Context<SubmitMessage>,
        _source_chain: String,
        _source_address: String,
        _message_type: String,
        _payload: Vec<u8>,
    ) -> Result<()> {
        // A signer alone is not a bridge attestation. This alpha implementation
        // has no source-domain allowlist, source-event proof, replay nonce, or
        // verifier binding. Reject atomically until an explicit verifier design
        // and trust configuration are implemented and independently reviewed.
        err!(BridgeError::AttestationVerificationUnavailable)
    }

    pub fn mark_processed(_ctx: Context<MarkProcessed>) -> Result<()> {
        // Processing is also disabled so no unverified historical record can be
        // promoted into a trusted bridge state.
        err!(BridgeError::AttestationVerificationUnavailable)
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer = authority,
        space = 8 + 32 + 8 + 8,
        seeds = [b"bridge"],
        bump
    )]
    pub bridge_state: Account<'info, BridgeState>,
    #[account(mut)]
    pub authority: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
#[instruction(source_chain: String, source_address: String, message_type: String)]
pub struct SubmitMessage<'info> {
    #[account(
        mut,
        seeds = [b"bridge"],
        bump,
        has_one = authority
    )]
    pub bridge_state: Account<'info, BridgeState>,
    #[account(
        init,
        payer = authority,
        space = 8 + 4 + 32 + 4 + 64 + 4 + 32 + 4 + 512 + 1 + 8,
        seeds = [b"msg", source_chain.as_bytes(), source_address.as_bytes(), bridge_state.total_messages.to_le_bytes().as_ref()],
        bump
    )]
    pub bridge_message: Account<'info, BridgeMessage>,
    #[account(mut)]
    pub authority: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct MarkProcessed<'info> {
    #[account(
        seeds = [b"bridge"],
        bump,
        has_one = authority
    )]
    pub bridge_state: Account<'info, BridgeState>,
    #[account(mut)]
    pub bridge_message: Account<'info, BridgeMessage>,
    pub authority: Signer<'info>,
}

#[account]
pub struct BridgeState {
    pub authority: Pubkey,
    pub total_messages: u64,
    pub created_at: i64,
}

#[account]
pub struct BridgeMessage {
    pub source_chain: String,
    pub source_address: String,
    pub message_type: String,
    pub payload: Vec<u8>,
    pub processed: bool,
    pub received_at: i64,
}

#[event]
pub struct BridgeMessageReceived {
    pub source_chain: String,
    pub source_address: String,
    pub message_type: String,
    pub received_at: i64,
}

#[event]
pub struct BridgeMessageProcessed {
    pub source_chain: String,
    pub source_address: String,
    pub processed_at: i64,
}

#[error_code]
pub enum BridgeError {
    #[msg("String field too long")]
    StringTooLong,
    #[msg("Payload too large")]
    PayloadTooLarge,
    #[msg("Message already processed")]
    AlreadyProcessed,
    #[msg("Bridge attestation verification is unavailable; message submission is fail-closed")]
    AttestationVerificationUnavailable,
    #[msg("Bootstrap is disabled until a source-backed partner authority design is approved")]
    BootstrapDisabled,
}
