import { NextResponse } from 'next/server';
import { validateApiKey, listProjectAssets, deleteProjectAsset } from '@/lib/storage/blob';
/**
 * List assets API
 * 
 * GET /api/assets?slug=<project-slug>
 * Headers:
 *   Authorization: Bearer <api_key>
 */ export async function GET(request) {
    try {
        const { searchParams } = new URL(request.url);
        const slug = searchParams.get('slug');
        if (!slug) {
            return NextResponse.json({
                error: 'Missing slug parameter'
            }, {
                status: 400
            });
        }
        // Validate API key
        const authHeader = request.headers.get('Authorization');
        const apiKey = authHeader?.replace('Bearer ', '');
        if (!apiKey) {
            return NextResponse.json({
                error: 'API key required'
            }, {
                status: 401
            });
        }
        const validatedSlug = await validateApiKey(apiKey);
        if (validatedSlug !== slug) {
            return NextResponse.json({
                error: 'Invalid API key for this project'
            }, {
                status: 403
            });
        }
        const assets = await listProjectAssets(slug);
        return NextResponse.json({
            success: true,
            slug,
            assets,
            count: assets.length
        });
    } catch (error) {
        console.error('[Assets API] Error:', error);
        const message = error instanceof Error ? error.message : String(error);
        return NextResponse.json({
            error: 'Failed to list assets',
            details: message
        }, {
            status: 500
        });
    }
}
/**
 * Delete asset API
 * 
 * DELETE /api/assets?slug=<project-slug>&fileName=<file-name>
 * Headers:
 *   Authorization: Bearer <api_key>
 */ export async function DELETE(request) {
    try {
        const { searchParams } = new URL(request.url);
        const slug = searchParams.get('slug');
        const fileName = searchParams.get('fileName');
        if (!slug || !fileName) {
            return NextResponse.json({
                error: 'Missing slug or fileName parameter'
            }, {
                status: 400
            });
        }
        // Validate API key
        const authHeader = request.headers.get('Authorization');
        const apiKey = authHeader?.replace('Bearer ', '');
        if (!apiKey) {
            return NextResponse.json({
                error: 'API key required'
            }, {
                status: 401
            });
        }
        const validatedSlug = await validateApiKey(apiKey);
        if (validatedSlug !== slug) {
            return NextResponse.json({
                error: 'Invalid API key for this project'
            }, {
                status: 403
            });
        }
        const deleted = await deleteProjectAsset(slug, fileName);
        if (!deleted) {
            return NextResponse.json({
                error: 'Asset not found'
            }, {
                status: 404
            });
        }
        return NextResponse.json({
            success: true,
            message: `Asset ${fileName} deleted`
        });
    } catch (error) {
        console.error('[Assets API] Error:', error);
        const message = error instanceof Error ? error.message : String(error);
        return NextResponse.json({
            error: 'Failed to delete asset',
            details: message
        }, {
            status: 500
        });
    }
}
