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

export async function GET() {
  try {
    // Get reported content
    const reports = await sql`
      SELECT 
        cr.id, cr.content_type, cr.content_id, cr.reason, cr.status, cr.created_at,
        u.name as reporter_name
      FROM content_reports cr
      LEFT JOIN users u ON cr.reporter_id = u.id
      WHERE cr.status = 'pending'
      ORDER BY cr.created_at DESC
      LIMIT 50
    `

    // Get content pending review (new tracks)
    const pendingTracks = await sql`
      SELECT 
        t.id, t.title, t.status, t.created_at,
        a.name as artist_name
      FROM tracks t
      LEFT JOIN artists a ON t.artist_id = a.id
      WHERE t.status = 'pending' OR t.status = 'under_review'
      ORDER BY t.created_at DESC
      LIMIT 20
    `

    return NextResponse.json({
      reports: reports.map((r: any) => ({
        id: r.id,
        type: r.content_type,
        contentId: r.content_id,
        reason: r.reason,
        status: r.status,
        reporter: r.reporter_name || "Anonymous",
        createdAt: new Date(r.created_at).toLocaleString("fr-FR"),
      })),
      pendingContent: pendingTracks.map((t: any) => ({
        id: t.id,
        type: "track",
        title: t.title,
        artist: t.artist_name || "Unknown",
        status: t.status,
        submittedAt: new Date(t.created_at).toLocaleString("fr-FR"),
      })),
    })
  } catch (error) {
    console.error("Moderation error:", error)
    return NextResponse.json({ error: "Failed to fetch moderation data" }, { status: 500 })
  }
}
