"""Tests for PDA derivation — cross-checked against Go implementation values."""

from solders.pubkey import Pubkey

from corpo.pda import (
    find_proposal_address,
    find_proposal_deposit,
    find_token_owner_record,
    find_vote_record_address,
)

# Known program ID and addresses from the demo setup.
PROGRAM_ID = Pubkey.from_string("GTesTBiEWE32WHXXE2S4XbZvA5CrEc4xs6ZgRe895dP")
REALM = Pubkey.from_string("8xG1CMTGHG8LZTRuoezGa1TZEdK6Wqf7mHexdXSNkubE")
MINT = Pubkey.from_string("3JfyTyC8HwLYiuEka7w1twvavMD5XLh5uyx3bcWaK5X1")
OWNER = Pubkey.from_string("GkT2WDSov98WNhrGR9MVcz2Bj18RZwtkwsutpGVkMJTL")


def test_token_owner_record_deterministic():
    """Same inputs always produce same PDA."""
    addr1, bump1 = find_token_owner_record(PROGRAM_ID, REALM, MINT, OWNER)
    addr2, bump2 = find_token_owner_record(PROGRAM_ID, REALM, MINT, OWNER)
    assert addr1 == addr2
    assert bump1 == bump2


def test_token_owner_record_different_owners():
    """Different owners produce different PDAs."""
    other_owner = Pubkey.from_string("11111111111111111111111111111111")
    addr1, _ = find_token_owner_record(PROGRAM_ID, REALM, MINT, OWNER)
    addr2, _ = find_token_owner_record(PROGRAM_ID, REALM, MINT, other_owner)
    assert addr1 != addr2


def test_proposal_deposit_deterministic():
    proposal = Pubkey.from_string("8ow36eMr4nby3oHrP3gGqFLD4GfZkHRYaNDtoDHChJrQ")
    addr1, b1 = find_proposal_deposit(PROGRAM_ID, proposal, OWNER)
    addr2, b2 = find_proposal_deposit(PROGRAM_ID, proposal, OWNER)
    assert addr1 == addr2
    assert b1 == b2


def test_proposal_address_deterministic():
    governance = Pubkey.from_string("Hzs4FaKqUp1FbaKDqiJEeQyZmWrKQL4gQRhRqaKc1g4N")
    seed = Pubkey.from_string("11111111111111111111111111111111")
    addr1, _ = find_proposal_address(PROGRAM_ID, governance, MINT, seed)
    addr2, _ = find_proposal_address(PROGRAM_ID, governance, MINT, seed)
    assert addr1 == addr2


def test_vote_record_deterministic():
    proposal = Pubkey.from_string("8ow36eMr4nby3oHrP3gGqFLD4GfZkHRYaNDtoDHChJrQ")
    tor, _ = find_token_owner_record(PROGRAM_ID, REALM, MINT, OWNER)
    addr1, _ = find_vote_record_address(PROGRAM_ID, proposal, tor)
    addr2, _ = find_vote_record_address(PROGRAM_ID, proposal, tor)
    assert addr1 == addr2


def test_pda_bump_is_valid():
    """Bump seed should be 0-255."""
    _, bump = find_token_owner_record(PROGRAM_ID, REALM, MINT, OWNER)
    assert 0 <= bump <= 255
