import { NextRequest, NextResponse } from 'next/server'

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { command, task, action } = body

    if (!command || !task) {
      return NextResponse.json(
        { error: 'Missing command or task' },
        { status: 400 }
      )
    }

    // Log the command - in production this would trigger Horus
    console.log(`[Task Command] Task: ${task}, Command: ${command}, Action: ${action}`)
    
    // Store command in a file for Horus to pick up
    const fs = require('fs')
    const path = require('path')
    const commandFile = path.join(process.cwd(), 'pending-commands.json')
    
    let commands = []
    if (fs.existsSync(commandFile)) {
      commands = JSON.parse(fs.readFileSync(commandFile, 'utf-8'))
    }
    
    commands.push({
      id: Date.now().toString(),
      task,
      command,
      action,
      createdAt: new Date().toISOString(),
      status: 'pending'
    })
    
    fs.writeFileSync(commandFile, JSON.stringify(commands, null, 2))

    return NextResponse.json({
      success: true,
      message: `Command sent for task: ${task}`,
      commandId: Date.now().toString()
    })
  } catch (error) {
    console.error('Error processing command:', error)
    return NextResponse.json(
      { error: 'Failed to process command' },
      { status: 500 }
    )
  }
}

export async function GET() {
  return NextResponse.json({ 
    status: 'ok',
    message: 'Command API ready'
  })
}
