//! Pyra PDA helpers.
//!
//! Throughout this crate, `owner` always refers to the end user's wallet
//! pubkey (the "Pyra owner"). Functions that derive PDAs which are scoped
//! to a Pyra vault take `owner` and derive the vault internally — callers
//! should not pre-derive the vault and pass it in.

use solana_program::pubkey::Pubkey;

use crate::PYRA_PROGRAM_ID;

/// Derive the Pyra vault PDA for a user.
///
/// `owner` is the end user's wallet pubkey.
pub fn get_vault(owner: &Pubkey) -> Pubkey {
    let (pubkey, _) = Pubkey::find_program_address(&[b"vault", owner.as_ref()], &PYRA_PROGRAM_ID);
    pubkey
}

/// Derive the Pyra deposit-address PDA for a user's vault.
///
/// `owner` is the end user's wallet pubkey. The Pyra vault is derived
/// internally — do NOT pass the vault.
pub fn get_deposit_address(owner: &Pubkey) -> Pubkey {
    let vault = get_vault(owner);
    let (pubkey, _) =
        Pubkey::find_program_address(&[b"deposit_address", vault.as_ref()], &PYRA_PROGRAM_ID);
    pubkey
}

pub fn get_spend_hold() -> Pubkey {
    let (pubkey, _) = Pubkey::find_program_address(&[b"spend_hold"], &PYRA_PROGRAM_ID);
    pubkey
}

pub fn get_swap_ledger(mint: &Pubkey) -> Pubkey {
    let (pubkey, _) =
        Pubkey::find_program_address(&[b"swap_ledger", mint.as_ref()], &PYRA_PROGRAM_ID);
    pubkey
}

pub fn get_swap_ledger_v2(mint: &Pubkey) -> Pubkey {
    let (pubkey, _) =
        Pubkey::find_program_address(&[b"swap_ledger_v2", mint.as_ref()], &PYRA_PROGRAM_ID);
    pubkey
}
