// 13XX Session Errors
const API_URL = "/api/";

const errorCodes = [
  { code: 1001, httpCode: 500, message: "Invalid request format" },
  { code: 1002, httpCode: 500, message: "Request data missing" },
  { code: 1003, httpCode: 500, message: "Invalid request parameter" },
  { code: 1004, httpCode: 500, message: "Request parameter missing" },
  { code: 1005, httpCode: 500, message: "Request limit exceeded" },
  { code: 1101, httpCode: 500, message: "Authentication required" },
  { code: 1102, httpCode: 500, message: "Invalid authentication credentials" },
  { code: 1103, httpCode: 500, message: "Authentication token expired" },
  { code: 1104, httpCode: 500, message: "Authentication token invalid" },
  { code: 1105, httpCode: 500, message: "Invalid username or password" },
  { code: 1201, httpCode: 500, message: "Resource not found" },
  { code: 1202, httpCode: 500, message: "Resource already exists" },
  { code: 1203, httpCode: 500, message: "Operation not permitted" },
  { code: 1204, httpCode: 500, message: "Access denied" },
  { code: 401, httpCode: 500, message: "Unauthorized access, please login" },
  { code: 1301, httpCode: 500, message: "Invalid input data" },
  { code: 1302, httpCode: 500, message: "Invalid data format" },
  { code: 1303, httpCode: 500, message: "Data not found" },
  { code: 1304, httpCode: 500, message: "Data already exists" },
  { code: 1305, httpCode: 500, message: "Invalid token, please login again" },
  {
    code: "1306",
    httpCode: 500,
    message:
      "The provided token is invalid or has expired. Please request a new token",
  },
  { code: 1307, httpCode: 500, message: "Email not found" },
  {
    code: 1308,
    httpCode: 500,
    message: "Invalid email or password. Please try again",
  },
  {
    code: 1309,
    httpCode: 500,
    message: "Invalid phone or password. Please try again.",
  },
  { code: 1310, httpCode: 500, message: "Invalid username format" },
  { code: 1311, httpCode: 500, message: "Username already taken" },
  { code: 1312, httpCode: 500, message: "Invalid password format" },
  { code: 1313, httpCode: 500, message: "Password too weak" },
  { code: 1314, httpCode: 500, message: "Password reset failed" },
  { code: 1315, httpCode: 500, message: "Invalid verification code" },
  { code: 1316, httpCode: 500, message: "Verification code expired" },
  { code: 1317, httpCode: 500, message: "Phone number not found" },
  { code: 1318, httpCode: 500, message: "Phone number already registered" },
  { code: 1319, httpCode: 500, message: "Invalid phone number format" },
  { code: 1320, httpCode: 500, message: "Phone number verification failed" },
  { code: 1321, httpCode: 500, message: "Email already registered" },
  { code: 1322, httpCode: 500, message: "Invalid email format" },
  { code: 1323, httpCode: 500, message: "Email verification failed" },
  { code: 1401, httpCode: 500, message: "Unauthorized" },
  { code: 1402, httpCode: 500, message: "Service unavailable" },
  { code: 1403, httpCode: 500, message: "Server overloaded" },
  { code: 1404, httpCode: 500, message: "Server timeout" },
  { code: 1405, httpCode: 500, message: "Request timeout" },
  { code: 1406, httpCode: 500, message: "Request canceled" },
  { code: 1407, httpCode: 500, message: "Server not responding" },
  { code: 1408, httpCode: 500, message: "API Key Is missing" },
  { code: 1501, httpCode: 500, message: "Database error" },
  { code: 1502, httpCode: 500, message: "Transaction failed" },
  { code: 1503, httpCode: 500, message: "Data inconsistency" },
  { code: 1504, httpCode: 500, message: "Lock wait timeout exceeded" },
  { code: 1505, httpCode: 500, message: "Deadlock detected" },
  { code: 500, httpCode: 500, message: "Internal Server Error" },
];

function findErrorByCode(errorCode) {
  const foundError = errorCodes.find((error) => error.code === errorCode);
  console.log(`ERROR ${foundError}`);
  return foundError != null
    ? foundError
    : { code: 500, message: "Internal server error", httpCode: 500 };
}

const getErrorMessage = (code) => {
  const error = findErrorByCode(code);
  return error;
};

module.exports = {
  getErrorMessage,
};
