// API: Create new page
import type { APIRoute } from 'astro';
import { createPage } from '@/lib/shim/pages';

export const POST: APIRoute = async ({ request }) => {
    try {
        // Token validation
        const authHeader = request.headers.get('Authorization');
        const token = authHeader?.replace('Bearer ', '');

        const godToken = import.meta.env.GOD_MODE_TOKEN;
        if (godToken && token !== godToken) {
            return new Response(JSON.stringify({ error: 'Unauthorized' }), {
                status: 401,
                headers: { 'Content-Type': 'application/json' }
            });
        }

        const data = await request.json();
        const page = await createPage(data);

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

    } catch (error: any) {
        console.error('Create page error:', error);
        return new Response(JSON.stringify({
            error: 'Failed to create page',
            message: error.message
        }), {
            status: 400,
            headers: { 'Content-Type': 'application/json' }
        });
    }
};
