import { NextResponse } from 'next/server';
import { headers } from 'next/headers';
import { getProjectContent, projectExists } from '@/lib/storage/blob';
/**
 * Debug endpoint to check multi-tenant routing
 * 
 * GET /api/debug?slug=acme-api-docs-i2rjwv
 */ export async function GET(request) {
    const headersList = await headers();
    const { searchParams } = new URL(request.url);
    const testSlug = searchParams.get('slug');
    // Collect all headers
    const allHeaders = {};
    headersList.forEach((value, key)=>{
        allHeaders[key] = value;
    });
    // Check for project header
    const projectSlug = headersList.get('x-devdoc-project');
    // Test Blob storage if slug provided
    let blobTest = null;
    if (testSlug) {
        try {
            const exists = await projectExists(testSlug);
            const content = exists ? await getProjectContent(testSlug) : null;
            blobTest = {
                slug: testSlug,
                exists,
                hasContent: !!content,
                filesCount: content?.files?.length || 0,
                projectName: content?.name || null
            };
        } catch (error) {
            blobTest = {
                slug: testSlug,
                error: String(error)
            };
        }
    }
    return NextResponse.json({
        timestamp: new Date().toISOString(),
        host: headersList.get('host'),
        projectSlug,
        hasBlobToken: !!process.env.BLOB_READ_WRITE_TOKEN,
        blobTest,
        headers: allHeaders
    });
}
