/**
 * System Tasks API
 * GET - List all system tasks
 * POST - Initialize system tasks
 */

import { NextRequest, NextResponse } from 'next/server';
import { getSystemTasks, initializeSystemTasks } from '@/lib/autonomy/system-tasks';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const category = searchParams.get('category') || undefined;

    const tasks = await getSystemTasks(category);

    return NextResponse.json({ tasks });
  } catch (error: any) {
    console.error('Error fetching system tasks:', error);
    return NextResponse.json(
      { error: 'Failed to fetch system tasks', details: error.message },
      { status: 500 }
    );
  }
}

export async function POST() {
  try {
    await initializeSystemTasks();
    const tasks = await getSystemTasks();

    return NextResponse.json({
      message: 'System tasks initialized',
      count: tasks.length,
      tasks,
    });
  } catch (error: any) {
    console.error('Error initializing system tasks:', error);
    return NextResponse.json(
      { error: 'Failed to initialize system tasks', details: error.message },
      { status: 500 }
    );
  }
}
