/**
 * BlockchainManager — abstraction layer for Web3 integration.
 * Currently a stub with localStorage fallback.
 * Future: replace with viem/ethers calls to Monad.
 */
export class BlockchainManager {
  constructor() {
    this.connected = false;
    this.address = null;
    this.bestScore = this.loadLocalBest();
  }

  async connect() {
    // TODO: integrate wallet (MetaMask, Rainbow, etc.) via viem
    // connect() stub
    this.connected = true;
    this.address = '0xStub...' + Math.random().toString(36).slice(2, 8);
    return this.address;
  }

  disconnect() {
    this.connected = false;
    this.address = null;
  }

  async submitScore(score) {
    // submitScore() stub
    if (score > this.bestScore) {
      this.bestScore = score;
      this.saveLocalBest(score);
    }
    return { txHash: '0x' + Math.random().toString(16).slice(2), confirmed: true };
  }

  async getLeaderboard(limit = 10) {
    // getLeaderboard() stub
    return [
      { rank: 1, address: '0xMonad...Dev', score: 99999 },
      { rank: 2, address: '0xAlice...xyz', score: 87500 },
      { rank: 3, address: '0xBob...abc', score: 74200 },
    ];
  }

  loadLocalBest() {
    const raw = localStorage.getItem('naddie_best_score');
    return raw ? parseInt(raw, 10) : 0;
  }

  saveLocalBest(score) {
    localStorage.setItem('naddie_best_score', String(score));
  }
}
