#!/usr/bin/env node
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');

const ACTIVATION_FILE = path.join(os.homedir(), '.reepoe', 'activation.json');

function loadActivation() {
  if (!fs.existsSync(ACTIVATION_FILE)) {
    console.error('❌ Not activated. Run: reepoe-start');
    process.exit(1);
  }
  return JSON.parse(fs.readFileSync(ACTIVATION_FILE, 'utf8'));
}

async function query(instruction) {
  const activation = loadActivation();
  const API_BASE = activation.api_base || 'http://localhost:8000';
  
  console.log(`🤖 ReePoe processing: "${instruction}"`);
  
  try {
    const response = await axios.post(`${API_BASE}/query`, {
      instruction,
      user_email: activation.email
    }, { timeout: 60000 });
    
    console.log(response.data.response || JSON.stringify(response.data, null, 2));
  } catch (error) {
    console.error('❌ Query failed:', error.response?.data?.detail || error.message);
    process.exit(1);
  }
}

const args = process.argv.slice(2);
if (args.length === 0) {
  console.log('Usage: reepoe query "your question"');
  process.exit(1);
}

query(args.join(' '));
