type DatabaseLikeError = Error & {
  code?: string;
  errno?: string | number;
  cause?: unknown;
};

export function getDatabaseErrorMessage(error: unknown): string {
  const databaseError = error as DatabaseLikeError | undefined;

  if (databaseError?.code === '28P01') {
    return 'Database authentication failed. Please verify DATABASE_URL username and password.';
  }

  if (databaseError?.code === '3D000') {
    return 'Database does not exist. Please verify the DATABASE_URL database name.';
  }

  if (databaseError?.code === 'ECONNREFUSED' || databaseError?.errno === 'ECONNREFUSED') {
    return 'Database connection was refused. Please ensure PostgreSQL is running and DATABASE_URL is correct.';
  }

  if (databaseError?.message?.includes('ENOTFOUND')) {
    return 'Database host could not be resolved. Please verify DATABASE_URL host settings.';
  }

  return 'Database connection failed. Please verify DATABASE_URL and that PostgreSQL is available.';
}
