/**
 * API Response Helpers
 *
 * Standardized HTTP response utilities for consistent API design
 * Reduces boilerplate and ensures proper status codes
 */

import { NextResponse } from "next/server";

/**
 * API Response Builder
 *
 * Provides type-safe, consistent response creation
 *
 * @example
 * ```typescript
 * // Before
 * return NextResponse.json({ error: 'Not found' }, { status: 404 });
 *
 * // After
 * return ApiResponse.notFound('Not found');
 * ```
 */
export class ApiResponse {
  /**
   * 200 OK - Successful GET request
   */
  static ok<T>(data: T, headers?: HeadersInit): NextResponse {
    return NextResponse.json(data, { status: 200, headers });
  }

  /**
   * 201 Created - Successful POST that created a resource
   */
  static created<T>(data: T, headers?: HeadersInit): NextResponse {
    return NextResponse.json(data, { status: 201, headers });
  }

  /**
   * 204 No Content - Successful request with no response body
   */
  static noContent(headers?: HeadersInit): NextResponse {
    return new NextResponse(null, { status: 204, headers });
  }

  /**
   * 400 Bad Request - Client error (validation, malformed request)
   */
  static badRequest(error: string, headers?: HeadersInit): NextResponse {
    return NextResponse.json({ error }, { status: 400, headers });
  }

  /**
   * 401 Unauthorized - Authentication required
   */
  static unauthorized(
    error: string = "Unauthorized",
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ error }, { status: 401, headers });
  }

  /**
   * 403 Forbidden - Authenticated but not authorized
   */
  static forbidden(
    error: string = "Forbidden",
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ error }, { status: 403, headers });
  }

  /**
   * 404 Not Found - Resource doesn't exist
   */
  static notFound(
    error: string = "Resource not found",
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ error }, { status: 404, headers });
  }

  /**
   * 409 Conflict - Resource conflict (duplicate, constraint violation)
   */
  static conflict(error: string, headers?: HeadersInit): NextResponse {
    return NextResponse.json({ error }, { status: 409, headers });
  }

  /**
   * 422 Unprocessable Entity - Validation failed
   */
  static unprocessableEntity(
    errors: Record<string, string>,
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ errors }, { status: 422, headers });
  }

  /**
   * 429 Too Many Requests - Rate limit exceeded
   */
  static tooManyRequests(
    error: string,
    retryAfter: number,
    additionalHeaders?: HeadersInit,
  ): NextResponse {
    const headers = new Headers(additionalHeaders);
    headers.set("Retry-After", retryAfter.toString());

    return NextResponse.json({ error, retryAfter }, { status: 429, headers });
  }

  /**
   * 500 Internal Server Error - Server-side error
   */
  static serverError(
    error: string = "Internal server error",
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ error }, { status: 500, headers });
  }

  /**
   * 502 Bad Gateway - Upstream service error
   */
  static badGateway(
    error: string = "Bad gateway",
    headers?: HeadersInit,
  ): NextResponse {
    return NextResponse.json({ error }, { status: 502, headers });
  }

  /**
   * 503 Service Unavailable - Service temporarily unavailable
   */
  static serviceUnavailable(
    error: string = "Service unavailable",
    retryAfter?: number,
    additionalHeaders?: HeadersInit,
  ): NextResponse {
    const headers = new Headers(additionalHeaders);
    if (retryAfter) {
      headers.set("Retry-After", retryAfter.toString());
    }

    return NextResponse.json({ error }, { status: 503, headers });
  }
}
