import { type NextRequest, NextResponse } from "next/server"
import { put } from "@vercel/blob"

export async function GET() {
  try {
    const databaseConfigured = !!(process.env.DATABASE_URL || process.env.POSTGRES_URL)
    const blobConfigured = !!process.env.BLOB_READ_WRITE_TOKEN

    return NextResponse.json({ 
      databaseConfigured,
      blobConfigured,
      provider: "vercel-blob"
    })
  } catch (error) {
    console.error("[Admin Storage API] Error fetching status:", error)
    return NextResponse.json({ error: "Erreur lors de la récupération du statut" }, { status: 500 })
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    
    if (body.action === "test") {
      // Test blob storage
      if (!process.env.BLOB_READ_WRITE_TOKEN) {
        return NextResponse.json({ error: "BLOB_READ_WRITE_TOKEN not configured" }, { status: 500 })
      }

      const testContent = `Storage test - ${new Date().toISOString()}`
      const blob = await put("test/storage-test.txt", testContent, {
        access: "public",
        addRandomSuffix: true,
      })

      return NextResponse.json({ 
        success: true, 
        testUrl: blob.url 
      })
    }

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error("[Admin Storage API] Error:", error)
    return NextResponse.json({ error: "Erreur lors du test" }, { status: 500 })
  }
}
