
import { NextRequest, NextResponse } from "next/server";
import db from "@/lib/db";
import { getLogger } from "@/utils/logger";

const logger = getLogger("/api/variants");

export const dynamic = "force-dynamic";



export const GET = async (req: NextRequest) => {
    try {
        const page = parseInt(req.nextUrl.searchParams.get("page") || "1", 10)
        const limit = parseInt(req.nextUrl.searchParams.get("limit") || "10")

        const skip = (page - 1) * limit

        console.log(page, limit, skip)

        const variants = await db.productVariant.findMany({
            include: {
                variant_images: true,
                product: true
            }
        })

        return NextResponse.json({ mssg: variants }, { status: 200 })
    }
    catch (error: any) {
        logger.error("Failed to fetch product variants", {
            error: error?.config?.data ?? error?.message ?? "Unknown error"
        })
        return new NextResponse("Internal Server Error", { status: 500 });
    }
}