import type { APIRoute } from 'astro';
import { system } from '@/lib/system/SystemController';

// GET: Retrieve current metrics and state
export const GET: APIRoute = async () => {
    const metrics = await system.getMetrics();

    return new Response(JSON.stringify(metrics), {
        status: 200,
        headers: { 'Content-Type': 'application/json' }
    });
};

// POST: Toggle state
export const POST: APIRoute = async ({ request }) => {
    try {
        const body = await request.json();

        if (body.action === 'toggle') {
            const newState = system.toggle();
            return new Response(JSON.stringify({
                success: true,
                state: newState,
                message: `System is now ${newState.toUpperCase()}`
            }));
        }

        return new Response(JSON.stringify({ error: 'Invalid action' }), { status: 400 });

    } catch (e: any) {
        return new Response(JSON.stringify({ error: e.message }), { status: 500 });
    }
};
