import prismadb from "@/lib/prismadb"
import { auth } from '@clerk/nextjs/server'
import { NextResponse } from "next/server"
import { v4 as uuidv4 } from 'uuid';

export async function POST (
    req: Request,
) {
    try {
        const {userId} = auth()
        const body = await req.json()

        const {name} = body

        if (!userId) {
            return new NextResponse("Unauthorized", {status:401})
        }

        if (!name) {
            return new NextResponse("Name is required", {status:401})
        }

        const store = await prismadb.store.create({
            data: {
                id: uuidv4(), // Generate a unique ID for the store
                name,
                userId,
                updatedAt: new Date().toISOString(), // Set the current timestamp for updatedAt
            }
        })

        return NextResponse.json(store)
    } catch (error) {
        console.log('[STORES_POST]', error)
        return new NextResponse("Internal error", {status: 500})
    }
}