// API: Update site by ID
import type { APIRoute } from 'astro';
import { updateSite } from '@/lib/shim/sites';

export const PUT: APIRoute = async ({ params, request }) => {
    try {
        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 site = await updateSite(params.id!, data);

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

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