-- ============================================================
-- Migration 007: MooChedda Wallet Integration
-- ============================================================

ALTER TABLE users
  ADD COLUMN IF NOT EXISTS moochedda_address VARCHAR(200) AFTER referred_by_user_id,
  ADD COLUMN IF NOT EXISTS moochedda_mnemonic TEXT AFTER moochedda_address,
  ADD COLUMN IF NOT EXISTS moochedda_private_key TEXT AFTER moochedda_mnemonic,
  ADD INDEX IF NOT EXISTS idx_moochedda_address (moochedda_address);

-- Track token grants so we can display history and avoid double-sends
CREATE TABLE IF NOT EXISTS token_grants (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL,
  from_address VARCHAR(200) NOT NULL,
  to_address VARCHAR(200) NOT NULL,
  token_symbol VARCHAR(20) NOT NULL,
  amount DECIMAL(18,6) NOT NULL,
  reason VARCHAR(255),
  tx_hash VARCHAR(255),
  status ENUM('pending','completed','failed') DEFAULT 'pending',
  error_message TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  INDEX idx_user (user_id),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
