import type { Response } from "express";
import type { ApiFailure, ApiSuccess } from "../@types/api.js";

export const sendSuccess = <T>(
  res: Response,
  status: number,
  data: T,
  message?: string,
) => {
  const payload: ApiSuccess<T> = { success: true, data };
  if (message) payload.message = message;
  return res.status(status).json(payload);
};

export const sendError = (
  res: Response,
  status: number,
  code: string,
  message: string,
  details?: unknown,
) => {
  const payload: ApiFailure = {
    success: false,
    error: {
      code,
      message,
      ...(details === undefined ? {} : { details }),
    },
  };
  return res.status(status).json(payload);
};
