// API: Create new post
import type { APIRoute } from 'astro';
import { createPost } from '@/lib/shim/posts';

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 post = await createPost(data);

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

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