import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';

export async function GET(
    request: NextRequest,
    { params }: { params: Promise<{ slug: string }> }
) {
    try {
        const { slug } = await params;

        const qrCode = await db.qRCode.findUnique({
            where: { slug },
            select: {
                id: true,
                content: true,
                contentType: true,
                status: true,
            },
        });

        if (!qrCode) {
            return NextResponse.json({ error: 'QR Code not found' }, { status: 404 });
        }

        if (qrCode.status === 'PAUSED') {
            return NextResponse.json({ error: 'QR Code is paused' }, { status: 403 });
        }

        return NextResponse.json({
            contentType: qrCode.contentType,
            content: qrCode.content,
        });
    } catch (error) {
        console.error('Error fetching public QR:', error);
        return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
    }
}
