import type { ApiResponse } from '#contracts/shared';

/**
 * Response factory for creating standardized API responses
 * Similar pattern to GenericError.create()
 */
export class SuccessResponse {
	static success<T>(data: T): ApiResponse<T> {
		return {
			success: true,
			trace_id: null,
			timestamp: new Date().toISOString(),
			errors: null,
			data: data as T,
		};
	}
}

/**
 * Sends a success response with standard envelope
 * Combines res.json() with SuccessResponse.success() for cleaner code
 */
import type { Res } from '#interfaces/IApi';

export function sendSuccess<T>(res: Res, data: T): Res;
export function sendSuccess<T>(res: Res, data: T, _messageVi?: string, _messageEn?: string): Res;
export function sendSuccess<T>(res: Res, data: T, _messageVi?: string, _messageEn?: string): Res {
	// Backward compatibility: ignore message parameters, they're no longer used
	return res.json(SuccessResponse.success(data));
}

/**
 * Creates a standard API response payload
 * @param data - The response data
 * @param requestId - Request trace ID (optional)
 * @returns Standard API response payload
 */
export function createResponse<T>(data: T): ApiResponse<T>;
export function createResponse<T>(data: T, requestId: string | null): ApiResponse<T>;
export function createResponse<T>(data: T, requestId?: string | null): ApiResponse<T> {
	return {
		success: true,
		trace_id: requestId ?? null,
		timestamp: new Date().toISOString(),
		errors: null,
		data: data as any,
	};
}
