import { db } from "@/lib/db"
import { NextResponse } from "next/server"
import { z } from "zod"

// get all other users
export async function GET(request: Request) {
    const { searchParams } = new URL(request.url)
    const id = searchParams.get("id") || undefined
    const idSchema = z.string().cuid().optional()

    const zod = idSchema.safeParse(id)

    if (!zod.success) {
        return NextResponse.json(zod.error, { status: 400 })
    }

    try {
        const users = await db.user.findMany({
            where: {
                NOT: {
                    id,
                },
            },

            orderBy: {
                createdAt: "desc",
            },

            select: {
                id: true,
                name: true,
                username: true,
                email: true,
                image: true,
                following: true,
                followers: true,
            },
        })

        return NextResponse.json(users, { status: 200 })
    } catch (error: any) {
        return NextResponse.json(error.message, { status: 500 })
    }
}