import { NextResponse } from "next/server"

export interface ApiResponse<T = any> {
  success: boolean
  data: T
  error: string | null
}

export function successResponse<T>(data: T, status = 200): NextResponse {
  return NextResponse.json(
    {
      success: true,
      data: Array.isArray(data) ? data : data,
      error: null,
    } as ApiResponse<T>,
    { status },
  )
}

export function errorResponse(error: string, status = 400): NextResponse {
  return NextResponse.json(
    {
      success: false,
      data: [],
      error,
    } as ApiResponse,
    { status },
  )
}

export function emptyResponse(status = 200): NextResponse {
  return NextResponse.json(
    {
      success: true,
      data: [],
      error: null,
    } as ApiResponse,
    { status },
  )
}

export function notFoundResponse(resource = "Resource"): NextResponse {
  return errorResponse(`${resource} not found`, 404)
}

export function unauthorizedResponse(message = "Unauthorized"): NextResponse {
  return errorResponse(message, 401)
}

export function serverErrorResponse(error?: any): NextResponse {
  console.error("[API] Server error:", error)
  return errorResponse(error?.message || "Internal server error", 500)
}
