import { NextResponse } from "next/server";

// Store messages in temp file
const QUEUE_FILE = "/tmp/horus-mc-queue.json";

async function sendToTelegram(text: string) {
  const token = process.env.TELEGRAM_BOT_TOKEN;
  if (!token) {
    console.log("No Telegram token");
    return null;
  }
  
  // Send to Haitham via Telegram
  const res = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      chat_id: "382315644",
      text: `💬 MC Chat:\n${text}`,
    }),
  });
  
  return res.json();
}

export async function POST(request: Request) {
  try {
    const { message } = await request.json();
    
    if (!message) {
      return NextResponse.json({ error: "Message required" }, { status: 400 });
    }

    // Forward to Telegram
    await sendToTelegram(message);
    
    return NextResponse.json({ status: "sent" });
  } catch (error) {
    console.error("Error:", error);
    return NextResponse.json({ error: "Failed" }, { status: 500 });
  }
}
