import { NextResponse } from "next/server"
import { query } from "@/lib/db"
import { getPortalSession, isPortalError } from "../portal-utils"

export async function GET() {
  const session = await getPortalSession()
  if (isPortalError(session)) return session

  if (!session.customerId) {
    return NextResponse.json({ projects: [] })
  }

  const result = await query(
    `SELECT id, name, description, status, start_date, end_date, budget, currency,
            created_at, updated_at
     FROM projects
     WHERE customer_id = $1 AND deleted_at IS NULL
     ORDER BY created_at DESC`,
    [session.customerId],
  )

  return NextResponse.json({ projects: result.rows })
}
