"""Shared test fixtures."""

import json
import os
from pathlib import Path

import pytest
from solders.keypair import Keypair


@pytest.fixture
def tmp_keypair(tmp_path):
    """Generate a temporary keypair file and return (Keypair, path)."""
    kp = Keypair()
    kp_path = tmp_path / "test-keypair.json"
    kp_path.write_text(json.dumps(list(bytes(kp))))
    kp_path.chmod(0o600)
    return kp, kp_path


@pytest.fixture
def tmp_config(tmp_path, tmp_keypair):
    """Generate a temporary config.toml referencing the test keypair."""
    _, kp_path = tmp_keypair
    cfg_path = tmp_path / "config.toml"
    cfg_path.write_text(f"""[identity]
keypair = "{kp_path}"

[network]
api_url = "http://localhost:8080"
rpc_url = "https://api.devnet.solana.com"
program_id = "GTesTBiEWE32WHXXE2S4XbZvA5CrEc4xs6ZgRe895dP"

[defaults]
realm = "8xG1CMTGHG8LZTRuoezGa1TZEdK6Wqf7mHexdXSNkubE"
governance = "Hzs4FaKqUp1FbaKDqiJEeQyZmWrKQL4gQRhRqaKc1g4N"
governing_token_mint = "3JfyTyC8HwLYiuEka7w1twvavMD5XLh5uyx3bcWaK5X1"
""")
    return cfg_path
