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

export async function GET() {
  const health = {
    status: "ok",
    timestamp: new Date().toISOString(),
    services: {
      database: { status: "unknown", message: "" },
      blob: { status: "unknown", message: "" },
    },
    environment: process.env.NODE_ENV || "development",
  }

  // Check database connection
  try {
    const result = await sql`SELECT 1 as connected`
    if (result && result.length > 0) {
      health.services.database = { status: "ok", message: "Connected to Neon" }
    }
  } catch (error: any) {
    health.status = "degraded"
    health.services.database = { 
      status: "error", 
      message: error.message || "Database connection failed" 
    }
  }

  // Check blob storage configuration
  if (process.env.BLOB_READ_WRITE_TOKEN) {
    health.services.blob = { status: "ok", message: "Blob token configured" }
  } else {
    health.status = "degraded"
    health.services.blob = { 
      status: "warning", 
      message: "BLOB_READ_WRITE_TOKEN not configured" 
    }
  }

  const statusCode = health.status === "ok" ? 200 : 503

  return NextResponse.json(health, { status: statusCode })
}
